how to refer to the text value? - php

$list_box_contents = array();
$list_box_contents[0] = array('params' => 'class="productListing-odd"');
$list_box_contents[0][] = array('params' => 'class="productListing-data"',
'text' => TEXT_NO_PRODUCTS);
i want to make a condition whether there is a value in $list_box_contents[0][]["text"] .when i write the code if(!empty($list_box_contents[0][]["text"])). my IDE alet me there is an error. what's wrong with it?

[] it's not a position, index or something like this.
With
$list_box_contents[0][] = array('params' => 'class="productListing-data"',
'text' => TEXT_NO_PRODUCTS);
you are pushing the array with its two value (and keys) on the right of the = in the last position of the subarray which has index 0. That is the structure you'll have is:
$list_box_contents[0] => array(VALUES-BEFORE, [last-position-key] => array('params' => 'class="productListing-data"', 'text' => TEXT_NO_PRODUCTS))
Anyway to have an idea of what happens, you can use print_r($list_box_contents) or var_dump($list_box_contents)
after the lines you posted.

When you assign something as in $list_box_contents[0][], you are assigning the contents of whatever follows to the next element in the array $list_box_contents[0]. So in this case, you would be assigning
array('params' => 'class="productListing-data"',
'text' => TEXT_NO_PRODUCTS);
to index 0 of $list_box_contents[0].
You can't refer to it using the $array[] notation, because the PHP parser doesn't have any idea which element in $array that you want to refer to. You need to specify.

Related

How to use build_query function of Guzzle?

I have to pass a query parameters with different values but absolutely equal keys. I found this \GuzzleHttp\Psr7\build_query(); But it returns only last value. For instance:
$array = [
'test' => '1',
'test' => '2'
]
\GuzzleHttp\Psr7\build_query($array);
//OR
http_query_builder($array);
It returns every time string with the last item.
Does it possible to do that with PHP? Because the side which will take these parameters just can not do anything in their side so I have to pass parameters with the equal keys.
The problem was not with the specific method used, but with how you filled your array to begin with:
$array = [
'test' => '1',
'test' => '2'
]
You can not use the same array key twice; your array only contains one element now, because the second one has overwritten the existing first one under that same key.
Make the test element itself an array, that contains your two values:
$array = [
'test' => ['1', '2']
];

How to properly use array_push for key and value that is also another array

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;

How to match this value?

So I am writing something to add on to my website.
I have this value stated above:
$settings[] = array(
"name" => "torblock_redirecturl",
"title" => $lang->redirecturl,
"optionscode" => "text",
"disporder" => 1,
"value" => denied.php,
"gid" => $gid
There will be a setting that where someone can enter another url or page. But I have this later in the php file:
die('$torblock_redirecturl');
I want that to change to the value in the setting once the setting is changed.
Aka once a value is entered in the setting, I want the value to change to whatever was entered right in the die.
Thanks for your answers!
You were assigning multidimensional array.
$settings[] = array( "value" => denied.php);
So to get the value access like $settings[0]['value']; the index 0 changes according to number of arrays stored in the $settings
To get expected
die($settings[0]['value']);
Update :
If assigning as single dimensional array.
$settings = array( "value" => denied.php);
Then access it like die($settings['value']);
Try this:
die($settings[0]['name']);
It would be easier to help if you supplied more context, but your syntax error could imply that you wrap an array variable in quotes without escaping, like so:
die('$settings['name']'); // this will break
If you prefer wrapping variables in quotes, you can do it like so:
die("{$settings[0]['name']}");
You may be aware of this, but in your code you're not just assigning an array to the $settings variable:
// this:
$settings[] = array('name' => 'foo');
// is the same as doing this:
$settings = array();
array_push($settings, array('name' => 'foo'));

pushing and apending arrays into an associative array in php

How do I push arrays inside the "adjacencies" key value pair that should have an encapsulated array holding the "arrays" (ie. array(("nodeTo" => "$to"),("nodeTo" => "$to"))) without overwriting them and appending them similiar to "+=". also the push into the key "adjacencies" doesnt seem to pick up the value.
$node[] = array(
"adjacencies" => array(), //inside this array should go all the arrays seprated by commas.
"data" => array(
"color" => $color1,
"type" => $type1
);
// this push doesnt seem to detect the adjacencies value and doesnt really push the array inside of the container array. I also tried $node["adjacencies"][]=array("nodeTo" => "$to"); but it didnt work
$node["adjacencies"]=array("nodeTo" => "$to");
}
If you want multiple arrays within 'adjacencies', append them to the end of the array:
$node[0]['adjacencies'][] = array("nodeTo" => "$to");
Granted, you'll need to know which $node index to work with (if there are multiple nodes).
Edit:
After reading the comments, it looks like the OP's desired array structure is this:
$node = array(
'adjacencies' => array(),
'data' => array(
'color' => $color1,
'type' => $type1,
);
);
Thus, to append additional nodes to the adjacencies array, you can do this:
$node['adjacencies'][] = array('nodeTo' => "$to");
By the way you use $node in the second statement I think you meant:
$node = array(
not:
$node[] = array(
// ^^
Then you can push the array by doing:
$node['adjacencies'][] = array('nodeTo' => $to);

Problem inserting string into array

i'm trying to insert an implode generated string to an array that then later be used for json implementation
the implode generated string is look like this
'id' => $this->_SqlResult[0],'UserId' => $this->_SqlResult[1],'Msg' => $this->_SqlResult[2],'MsgStamp' => $this->_SqlResult[3]
i would like to used it in this code
$this->_JsonArr[]=array($Generated string);
to achieve something like this
$this->_JsonArr[]=array('id' => $this->_SqlResult[0],'UserId' => $this->_SqlResult[1],'Msg' => $this->_SqlResult[2],'MsgStamp' => $this->_SqlResult[3]);
instead i got something like this
$this->_JsonArr[]=array(" 'id' => $this->_SqlResult[0],'UserId' => $this->_SqlResult[1],'Msg' => $this->_SqlResult[2],'MsgStamp' => $this->_SqlResult[3]");
seem like generated string is treated as one element as key and value pair.
obviously i can get expected output from mysql because of this, can anybody help me with this
Why do you need to implode anything? Just pass the array:
$this->_JsonArr[] = your-non-imploded-array-here;
I think a full solution to what you want to do is something like this (i.e., the third code box in your question):
$row = array(
'id' => $this->_SqlResult[0],
'UserId' => $this->_SqlResult[1],
'Msg' => $this->_SqlResult[2],
'MsgStamp' => $this->_SqlResult[3]
);
$this->_JsonArr[] = $row;
$this->_JsonArr[]=array($Generated
string);
Looks like you want use arrays keys and values, but as I see you put into array plain string with expectation that array parse your plain string in format: keys => values.
You can try create array like below:
$this->_JsonArr[ $Generated_key ] = array( $Generated_value );
(Please correct me if I wrong understand your question).

Categories