I have my array looking like this when I print_r(delegates[$i])
Array
(
['firstName'] => Brady
['surname'] => Manning
['jobTitle'] => CEO
['memberNumber'] => 123456
['dieteryRequirements'] => halal
)
Howerver, when I try to get the firstName like this echo delegates[$i]['firstName'] I get the following error
Notice: Undefined index: firstName
Schoolboy question but I'd really appreciate some help here.
After examining the error you are having, this is my best answer. I initially assumed you didnt need the index $i, but that was not the case since you are actually getting results when you print_r($delegates[$i]) Therefore I am lead to believe your array is a multidimensional array.
Another thing I noticed, (and I give credit to #Rizier123 who pointed out in the comments to use both single and double quotes) is that your print_r result is outputting single quotes '' around your element keys like this 'firstname' This means that you are actually storing the quotes inside your array. With all that said I believe your $delegates array looks something like this:
$delegates = array(
array(
"'firstName'" => 'Brady',
"'surname'" => 'Manning',
"'jobTitle'" => 'CEO',
"'memberNumber'" => 123456,
"'dieteryRequirements'" => 'halal',
)
);
Therefore in order to access the element you will need to use the index, and use the element with the single quotes '' like this:
echo $delegates[0]["'firstname'"]
What I would do is remove all those single quotes so that you can access them correctly.
Hope this helps.
Related
I have a problem with the array PHP function, below is my first sample code:
$country = array(
"Holland" => "David",
"England" => "Holly"
);
print_r ($country);
This is the result Array ( [Holland] => David [England] => Holly )
I want to ask, is possible to make the array data become variables? For second example like below the sample code, I want to store the data in the variable $data the put inside the array.:
$data = '"Holland" => "David","England" => "Holly"';
$country = array($data);
print_r ($country);
But this result is shown me like this: Array ( [0] => "Holland" => "David","England" => "Holly" )
May I know these two conditions why the results are not the same? Actually, I want the two conditions can get the same results, which is Array ( [Holland] => David [England] => Holly ).
Hope someone can guide me on how to solve this problem. Thanks.
You can use the following Code.
<?php
$country = array(
"Holland" => "David",
"England" => "Holly"
);
foreach ($country as $index => $value)
{
$$index = $value;
}
?>
Now, Holland & England are two variables. You can use them using $Holland etc.
A syntax such as $$variable is called Variable Variable. Actually The inner $ resolves the a variable to a string, and the outer one resolves a variable by that string.
So there is this thing called
Destructuring
You can do it something like ["Holland" => $eolland, "England" => $england] = $country;
And now you have your array elements inside the variables.
Go read the article above if you want more information about this because it gets really useful (especially in unit tests usind data provders from my experience).
If you want to extract elements from an associative array such that the keys become variable names and values become the value of that variable you can use extract
extract($country);
To check what changed you can do
print_r(get_defined_vars());
Explanation on why the below does not work
$data = '"Holland" => "David","England" => "Holly"';
When you enclose the above in single quotes ' php would recognise it as a string. And php will parse a string as a string and not as an array.
Do note it is not enough to create a string with the same syntax as the code and expect php to parse it as code. The codes will work if you do this
$data = ["Holland" => "David","England" => "Holly"];
However, now $data itself is an array.
A simple copy of an array can be made by using
$data = $country;
This is the print_r from the $form_data variable / array in php log.
[28-Sep-2018 18:04:03 UTC] Array
(
[cfdb7_status] => unread
[meno] => data
[email] => data
[telefon] => phone
[meno-ucastnika__1] => data
[email-ucastnika__1] => data
[meno-komory__1] =>
[registracne-cislo__1] =>
[_wpcf7_groups_count] => Array
(
[emails] => 1
)
[obchodne-meno] => obchod
[obchodne-sidlo] => fs
[ico] => 50426508
[dic] => dic
[icdph] => icdicko
)
How can I get the value of _wpcf7_groups_count key?
If I want email I simply wrote $form_data['email']. Everything goes like this except _wpcf7_groups_count.
$form_data['_wpcf7_groups_count']
$form_data['_wpcf7_groups_count'][0]['emails']
$form_data['_wpcf7_groups_count']['emails']
Anything from above doesn't work. The first is giving me an illegal offset.
From the data you posted(*),
$form_data['_wpcf7_groups_count']['emails']
should work, and yield 1.
Note that the parent key value is an array, so if the form data goes through some sort of templating engine, the problem might lie there.
I find it strange that you get an error for the first method, and not for the others: in PHP, if you can't reference an array key, you cannot reference any of the descendants, and you still get the error from the parent key. This is what makes me suspect that something else is afoot.
==========
(*) I assumed that you have deleted some information while keeping the formatting. Otherwise writing
[meno-komory__1] =>
[registracne-cislo__1] =>
could possibly be interpreted as a nested key. I saw no 'Array', so I assumed there was just some data missing. But next time write it explicitly to avoid any ambiguity:
[meno-komory__1] => "(redacted)",
[registracne-cislo__1] => "(array, redacted)",
I have two arrays, which I can either do the simple approach, which is do a For Each loop on each array, and if one way is the method, accept it, or vice-versa, or is it possible to set values in an array with the same key value?
Like:
<?php
$Array = Array('UniqueValue' => 'Key',
'UniqueValue_2' => 'Key',
'DifferentValue' => 'Key_2'); // and etc...
?>
I could probably try trial and error, but another method would be somehow integrating the arrays and only reading the values I need?
Thanks for your guys' time.
ah, quick edit. I feel like someone's going to bring this up, so I read Nested array, get items with same key and I tried, but it's not working for me. It reads only the first value, and I'm not breaking the loop. If you want to see the code I'm working with, I'll gladly add it.
Actually, I found a better way to word this, so!
I need to iterate through each array value with the same key in a For Each loop.
Like so:
ForEach($Array as $Key){ // or $Key => $Value
If($Key == 'Key'){
Echo $Value;
}
}
<?php
$Array = Array(
0 => Array('Test', 'Testing', 'Tester'),
1 => Array('Test2'));
Print_R($Array[0]); // Array ( [0] => Test [1] => Testing [2] => Tester )
?>
Ah, I forgot about nesting arrays. Sorry about that, haha, but for anyone who would possibly need an answer, here you go.
$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.
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).