0

I have a big array which looks like this:

array(2) {
  ["Final Fantasy VII"]=>
  array(5) {
    ["rows"]=>
    array(2) {
      [0]=>
      array(6) {
        ["price"]=>
        string(5) "11.69"
        ["price_old"]=>
        string(4) "4.66"
        ["currency"]=>
        string(4) "euro"
        ["portal"]=>
        string(0) ""
        ["link"]=>
        string(77) "https://de.gamesplanet.com/game/final-fantasy-vii-download--1001-1?ref=gmkeys"
        ["shop"]=>
        string(4) "9507"
      }
      [1]=>
      array(6) {
        ["price"]=>
        string(5) "14.99"
        ["price_old"]=>
        ...
      }
    }
  }

 ["Battlefield 1"]=>
  array(3) {
    ["rows"]=>
    array(2) {
      [0]=>
      array(6) {
        ["price"]=>
        ...
      }
      [1]=>
      array(6) {
        ["price"]=>
        ...
      }
    }
  }
}

And I want to get only certain parts of this array where the name is matching my searched title. So, I use this code for that:

function createACFRepeater($title){
    $repeater = array();

    if(searchForGameXML($title)){

        $count = count($GLOBALS["productsXML"][$title]['rows']);

        for($i = 0; $i < $count; $i++){
            array_push($repeater, $GLOBALS["productsXML"][$title]['rows'][$i]);
        }

        return $repeater;
    }else{
        return $repeater;
    }
}

My problem now is that the the $repeater array looks like this:

array(2) {
  [0]=>
  array(6) {
    ["price"]=>
    string(5) "19.98"
    ["price_old"]=>
    ...
  }
  [1]=>
  array(6) {
    ["price"]=>
    string(4) "7.99"
    ["price_old"]=>
    ...
  }
}

There is a numeric key which is pointing to the array [0] => .... But what I want is simply an array in a array without any associative relations...

How can I create an array which looks like this:?

array(2) {
      array(6) {
        ["price"]=>
        string(5) "19.98"
        ["price_old"]=>
        ...
      }
      array(6) {
        ["price"]=>
        string(4) "7.99"
        ["price_old"]=>
        ...
      }
    }

Greetings and Thank You!

2 Answers 2

1

According to the array definition it is impossible. Any array item must have key and value, documentation for array starts from:

An array in PHP is actually an ordered map. A map is a type that associates values to keys

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

7 Comments

I need this array for a plugin. Watch here: advancedcustomfields.com/resources/update_field. And there, the array looks like this: $value = array( array( "sub_field_1" => "Foo1", "sub_field_2" => "Bar1", "acf_fc_layout" => "layout_1_name" ), array( "sub_field_x" => "Foo2", "sub_field_y" => "Bar2", "acf_fc_layout" => "layout_2_name" ) ); But maybe you have an answer to this one also? support.advancedcustomfields.com/forums/topic/…
right! but if you write $value[0] you receive ` array( "sub_field_1" => "Foo", "sub_field_2" => "Bar" )`. If you omit indexes they are auto-incremented. It does not mean that array does not have indexes at all
okay, thank you! - I got confused by the documentation of the ACF plugin
I changed it. It was easier for me to read beacuse of the examples... Do you have a idea to the ACF support link?
@lubart your link isn't working properly, you added http:// at the end of the link :)
|
1

You will always have numeric keys. As @lubart already said: it's impossible to have an array without keys. Btw., all of the the follwing arrays are completely equal:

$array1 = array([0] => array([0] => 'hi', [1] => array([0] => '23.5')));

$array2 = array(array('hi', array('23.5')));

$array3 = [['hi', ['23.5']]];

$array4 = [ [0] => [ [0] => 'hi', [1] => [ [0] => '23.5' ] ] ];

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.