0

I have an array that for some reason keeps kicking back an error "Illegal String Offset" I have tried several ways to loop through it but none seem to work.

$fields = Array (
[cookies] => Array (
    [timeout] => 0
    [votes_per_cookie] => 1
    [enabled] => on
    )
[ip] => Array (
    [timeout] => 0
    [filter] => 
    )
[direct] =>  Array ( )
[membership] => Array (
    [type] => Array ( )
    [once] =>  Array ( )
    )
[captcha] =>  Array ( )
[quota] => Array (
    [votes] => 1000
    )
[date] => Array (
    [start] => 1553846400
    [end] => 1554767940
    [enabled] => on
    )
[selection] => Array (
    [minimum] => 1
    [maximum] => 1
    )
[unique_id] => 1547834405
[results] => Array (
    [require_vote] => Array (
        [enabled] => on
        )
    )
);

I am trying to get the value of end in the date array. I have tried this:

foreach($fields as $field) {
  echo $field['end'];
}

this outputs the data but also the illegal offset error.

I have tried this:

foreach($fields as $key => $value) {
  echo $value['end'];
}

This also outputs the same reponse.

if I echo key using the above loop, it returns the appropriate keys (cookies, ip, direct, membership, etc)

I have also tried:

foreach($fields['date'] as $field) {
    $field['end'];
  }

This is just an error.

And finally

foreach($fields as $field) {
  $field['date']['end'];
}

just an error.

3
  • 1
    try to simple echo without foreach as echo $fields['date']['end']; Commented Jun 1, 2019 at 6:06
  • @BalvinderSingh yep....that did it. thanks! Commented Jun 1, 2019 at 6:08
  • Great! Welcome @RiotAct Commented Jun 1, 2019 at 6:09

4 Answers 4

1

Reason: end is the key of child array of date index (which is an index of parent array), so you have to use date also to fetch child-key value

Solution:

echo $fields['date']['end'];
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to iterate, just access the fields directly:

echo $fields['date']['end'];

Comments

1

You don't need to loop the array since you wanna get specific key value:

First method :

echo $fields['date']['end'];

second method : this little bit ugly the method.

echo array_column($fields,'end'))[0];

Comments

0

Your array should look like this in order to loop through. I've added quotation strings ("") to your indexes and to your on value. Else you would get a warning: like this 'Use of undefined constant on'.Hope this helps!.

     $fields = array (
      'cookies' => array ('timeout' => 0,'votes_per_cookie' => 1,'enabled' => 'on'),
      'ip'=> array ('timeout' => 0,'filter' => '',),
      'direct' =>  array (),
      'membership' => array ('type' => array (),'once' =>  array ()),
      'captcha' =>  array (),
      'quota' => array ('votes' => 1000,),
      'date' => array ('start' => 1553846400,'end' => 1554767940,'enabled' => 'on',),
      'selection' => array ('minimum' => 1,'maximum' => 1,),
      'unique_id' => 1547834405,
      'results' => array ('require_vote' => array ('enabled' => 'on'))
    );

Now you can loop through it properly with a foreach:

foreach($fields as $key => $value) {
  if($key == 'date'){
    print_r($value);
  }
}

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.