I want to create a multidimensional array on my code. I wrote something like below:
$game[$game_id]['map'][$place][0]
But I'm getting these errors:
-Illegal string offset 'map'
-Uninitialized string offset
-Undefined offset
Last 2 errors are for $place.
What's wrong?
You cant create the whole array in one step.
You get these errors, because $game[$game_id] seems to be an string and not an array. So ['map'] tries to accesss the 'map' index of an string which causes the Illegal string offset warning. The other errors are just an result of this, because $game[$game_id]['map'] returns null(and the warning)
$game = array(
$game_id => array(
'map' => array(
$place = array(...)
)
)
);
This should work
Related
So I am using this code to push new user IDs into an array but I get
Warning: array_push() expects at least 2 parameters, one given
$post_likes = array(
"Some key" => array(
'date' => date("d/m/Y"),
'IP' => get_client_ip())
);
$new_likes = array(
'date' => date("d/m/Y"),
'IP' => get_client_ip());
array_push($post_likes[$current_user->ID] = $new_likes);
The code works. It pushes new key with array value into the previous array. But still I get that warning. What am I missing?
Instead of using array_push() you can directly do like this-
$post_likes[$current_user->ID] = $new_likes;
A sample hardcoded example:- https://eval.in/1000261
Set your new variable $new_likes as like this:
array_push($post_likes[$current_user->ID] = $new_likes,$new_likes); //expects at least 2 parameters
More info : http://php.net/manual/en/function.array-push.php
Updated:
I am proffering and suggesting to use #Magnus Eriksson's answer though you have accepted my answer.
So, just use as like below:
$post_likes[$current_user->ID] = $new_likes;
I created a function called "keyword_query()" which gets a single string and runs a query with this variable on an API - then the API returns an array (which is defined global so it's changed outside the function too).
The second function "keyword_query_array()" should run the "keyword_query()" more than once, and push to a new array (which is global too) and get an array of several arrays. This function gets a variable of an array of keywords. The function gets the array and navigates through the array without any problem.
Please notice the comments in the code:
<?php
// Runs the query "Research Key" on a keyword and get App ids, names, ect'.
function keyword_query($keyword){
global $research_key_array, $keyword;
// Add the keyword to the "Research Key" query:
$research_key_query = "https://example.com/api/banana/ajax/kaka?term=$keyword&country=US&auth_token=666";
// Create a stream for Json. That's how the code knows what to expect to get.
$context_opts = array(
'http' => array(
'method' => "GET",
'header' => "Accepts: application/json\r\n"
)
);
$context = stream_context_create($context_opts);
// Get the Json
$research_key_json = file_get_contents($research_key_query, false, $context);
// Turn Json to a PHP array
$research_key_array = json_decode($research_key_json, true);
//var_dump($research_key_array);
//print_r($research_key_array);
return $research_key_array;
}
// Runs the keyword_query() function on an array of keywords.
function keyword_query_array($keyword_array){
global $array_of_key_queries;
// Get the last array cell
$last_array_cell = count($keyword_array);
// Navigate through the array
for ($i=0; $i<=$last_array_cell ; $i++) {
//echo $keyword_array[$i]; ****works!
// Error here: Notice: Undefined offset: 3 in C:\wamp\www\PHPExcel\api_fun.php on line 51
array_push( $array_of_key_queries, keyword_query($keyword_array[$i]) );
}
var_dump($array_of_key_queries);
}
But when I get to this line:
array_push( $array_of_key_queries, keyword_query($keyword_array[$i]) ); I get an error of:
Notice: Undefined offset: 3 in C:\wamp\www\PHPExcel\api_fun.php on line 51
with this var_dump:
array (size=4)
0 =>
array (size=1)
'keyword' =>
array (size=0)
empty
1 =>
array (size=1)
'keyword' =>
array (size=0)
empty
2 =>
array (size=1)
'keyword' =>
array (size=0)
empty
3 =>
array (size=1)
'keyword' =>
array (size=0)
empty
What is the right way to push an array within an array like this case?
I changed global $research_key_array, $keyword; from the first function to global $research_key_array;
Works great now!
Thanks #Adelphia !
This question already has answers here:
Illegal String Offset within PDO For Each loop
(2 answers)
Closed 8 years ago.
After completing a SELECT query on a MySQL database and applying the fetchAssoc() method to the result, I am receiving warnings when attempting to access the results in a foreach loop. Here is the code:
$query=$subquery->execute()->fetchAssoc();
foreach($query as $result) {
if ($result['active'] == 'Y'){ // (this is line 703)
$page_id = $result['page_id']; // (this is line 704)
if (!$package_pages[$page_id] || $package_pages[$page_id] != $page_id) { // line 705
$pages[] = $page_id;
}
}
I inserted var_dump($query) to inspect the results of the query. Here is an example of the output:
array (size=3)
'page_name' => string 'Apts & Rentals' (length=14)
'page_id' => string '49' (length=2)
'active' => string 'Y' (length=1)
array (size=3)
'page_name' => string 'Homepage' (length=8)
'page_id' => string '1' (length=1)
'active' => string 'Y' (length=1)
There are 25 arrays output from var_dump($query)
And here is a sample of the warnings:
Warning: Illegal string offset 'active' in ads_load() (line 703 of ...
Warning: Illegal string offset 'page_id' in ads_load() (line 704 of ...
Notice: Undefined index: Y in ads_load() (line 705 of ...
Why are the offsets 'active', and 'page_id' being flagged as illegal?
Where is the "Notice: Undefined index: Y" coming from since it is not being used as an index on line 705?
I don't believe this is a duplicate of Illegal String Offset within PDO For Each loop. The issue was resolved when I realized the code given above was placed within another loop which gave the impression I was dealing with an array of arrays rather than a single array each time.
You're accessing them incorrectly - you've got an associative array and you're trying to loop through it as if it were an array of associative arrays. This is your dumped data structure:
$query = array(
'page_name' => 'Homepage',
'page_id' => '1',
'active' => 'Y' );
You can get the results directly from $query, i.e.:
$query=$subquery->execute()->fetchAssoc();
if ($query['active'] == 'Y')
$page_id = $query['page_id'];
// ... the rest of your code ...
}
You could iterate through the keys and values of $query, but since you are interested in particular values from the $query array, there isn't much point.
I'm trying to see if you can assign an array value during creation to a value in the same array.
If I'm declaring and initializing an array like this:
$this->array = array(...);
Can I do something like this?
$this->array = array
(
'value1' => 'hello',
'value2' => $this->array['value1'],
);
I've tried it already and I get back
Notice: Undefined index: value1 in /Sites/website/config.php on line 329
I've gotten around this problem already but now out of interest I want to see if theres actually a way to do this.
It has to be during creation and declared like above, not through
$array['value1'] = 'foo';
$array['value2'] = $array['value1'];
or
$common = 'foo';
$array = array('value1' => $common, 'value2' => $common);
Is this technically impossible or is there any way?
I don't think it's possible. The array has to be parsed before it can be assigned, so at the time $this->array['value1'] is being calculated, $this->array is still null.
Basically, $this->array['value1'] cannot be be accessed before $this->array['value2'] is set, which needs to access $this->array['value1'], which cannot be accessed before . . .
The reason you're getting Notice: Undefined index: value1 in /Sites/website/config.php on line 329 is because when you're defining your array the value you're trying to access has not yet been defined because you haven't reached the end of the initial function.
In the following code:
$this->array = array
( //this is the start of the array
'value1' => 'hello',
'value2' => $this->array['value1'],
); //this is the end of the array statement.
The array is not defined until you reach the end of the statement.
The reason the other methods work is because you're using multiple steps to access an already existing variable.
I think that is not possible, assign the element when the array starts
you can set as false and before the action that set the value as your code.
example :
$common = 'foo';
$array = array(
'value1' => $common,
'value2' => $common
);
or you can use the function array_combine
I suddenly stuck here:
$source = (object) array(
'field_phone' => array(
'und' => array(
'0' => array(
'value' => '000-555-55-55',
),
),
),
);
dsm($source);
$source_field = "field_phone['und'][0]['value']";
dsm($source->{$source_field}); //This notation doesn't work
dsm($source->field_phone['und'][0]['value']); //This does
dsm() is Drupal developer function for debug printing variables, objects and arrays.
Why $source object doesn't understand $obj->{$variable} notation?
Notice: Undefined property: stdClass::$field_phone['und']['0']['value']
Because your object does not have a property that is named "field_phone['und'][0]['value']". It has a property that is named "field_phone" which is an array which has an index named "und" which is an array which has an index 0 and so on. But the notation $obj->{$var} does not parse and recursively resolve the name, as it shouldn't. It just looks for the property of the given name on the given object, nothing more. It's not like copy and pasting source code in place of $var there.