0

Hello I am setting a key value pair in an array in a foreach loop

e.g

array(2) {
  [0]=>
  array(1) {
  ["resourceType"]=>
  string(4) "File"
  ["resourceName"]=>
  string(4) "Test"

  [1]=>
  array(1) {
  ["resourceType"]=>
  string(4) "File"
  ["resourceName"]=>
  string(4) "Test"
 }

I am doing this via a foreach loop

foreach ($output as $data) {



$resourceType = strpos($data, "d");

if ($resourceType) {

    $ftpArray[]['resourceType'] = "Folder";
} else {

    $ftpArray[]['resourceType'] = "File";
}

$resourceName = strrchr($data, " ");

$resourceName = trim($resourceName);

if ($resourceName != ".." && $resourceName != "." && $resourceName != "") {

    $ftpArray[]['resourceName'] = $resourceName;

}

}

But the output is this

[0]=>
array(1) {
["resourceType"]=>
string(4) "File"
}
[1]=>
array(1) {
["resourceType"]=>
string(4) "Test"
}
[2]=>
array(1) {
["resourceType"]=>
string(4) "File"
}
[3]=>
array(1) {
["resourceName"]=>
string(9) ".htaccess"
}

Rather than the example I gave at the start of the question. How can I get the array to fill in key values pairs like the first example.

3 Answers 3

1

Make an tmp array

foreach ($output as $data) { 
  $a = array();
  if (strpos($data, "d")) { 
    $a['resourceType'] = "Folder"; 
  } else { 
    $a['resourceType'] = "File"; 
  } 
  $resourceName = trim(strrchr($data, " ")); 
  if ($resourceName != ".." && $resourceName != "." && $resourceName != "") { 
    $a['resourceName'] = $resourceName; 
  } 
  $ftpArray[] = $a; 
} 

Each calling of $ftpArray[] = 'x' adds new item to the array. It does not metter if you add there some second dimension key.

Sign up to request clarification or add additional context in comments.

Comments

0

You want to add a datastructure to a array. This, create the datastructure, do your stuff and add it to the array:

foreach ($output as $data) {
  $struct = array('resourceType' = > '', 'resourceName' => '');

  // do stuff, on the struct

  $resourceType = strpos($data, "d");
  if ($resourceType) {
    $struct['resourceType'] = "Folder";
  } else {
    $struct['resourceType'] = "File";
  }

  $resourceName = strrchr($data, " ");
  $resourceName = trim($resourceName);

  if ($resourceName != ".." && $resourceName != "." && $resourceName != "") {
    $struct['resourceName'] = $resourceName;
 }
 $ftpArray[] = $struct;
}

Notice that there is a sub tile difference with the previous answer, as the structure is always created.

Comments

0

Each [] operation on array adds a new element to the loop, so you would need to create a temporary value and then add it to the loop:

$element = array();
// set the data here
$output_array[] = $element

And second thing with that script string positions start with 0, so if you need to know that character was not found when using strpos you should check the return value with === or !== for FALSE.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.