0

So I'm iterating over this $elements array. I am trying to get the key and value from each individual item of the $elements array, which is also an array (which I will call $element).

Here's my code:

  $options = array();  
  foreach ($elements as $element) {
    if ($element['#active']) {
      continue;
    }


    $options[$element]['#query'] = $element['#indexed_value'];



  }

This doesn't work. How would I get the value of $element['#query'] and $element['indexed_value'] as a key => value pair?

3
  • $options[$element['#query']]=... Commented Feb 11, 2014 at 19:20
  • Can you provide a sample of the array that you're iterating through, and a sample of the expected results? Commented Feb 11, 2014 at 19:55
  • At first glance, you are using $element as an array and as an array key. It does not matter what your script is doing; it will not work. Commented Feb 11, 2014 at 19:55

2 Answers 2

1

try this

 $options[$element['#query']] = $element['#indexed_value'];

I execute following code

 $options["junaid"] = "hassan";
 echo print_r($options, true);

Got following result

 Array
 (
     [junaid] => hassan
 )
Sign up to request clarification or add additional context in comments.

2 Comments

It gives me, "array()" when I print_r($options, true);
did you echo $element['#query'] and $element['#indexed_value'] ? what values they have ?
0

Hmm If I understand the problem:

$elements = array(array('a' => 1, 'b' => 2, 'c' =>3), array('d' =>4, 'e' =>5,'f' =>6) );
$options = array();
$temp = array();
$temp2 = array();

//value is each array in $elements
foreach ($elements as $element) {       
    $temp = array_merge($temp, array_keys($element));   
    $temp2 = array_merge($temp2, array_values($element));   
}
$options = array_combine($temp, $temp2);
var_dump($options);

and it produces this output:

array(6) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) ["d"]=> int(4) ["e"]=> int(5) ["f"]=> int(6) } 

I used this website

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.