I tried to access an object property which has array form using string variable but not able do so when the string contains index selection. Take a look at this example.. what i was tring to do is to use $access3 like string
$dummy = new stdClass();
$dummy->testNormal = 'itemNormal';
$dummy->testArray = array('item1', 'item2');
$access1 = 'testNormal';
$access2 = 'testArray';
$access3 = 'testArray[0]';
echo 'try access1: ' . $dummy->{$access1} . '<br />';
echo 'try access2: ' . $dummy->{$access2}[0] . '<br />';
echo 'try access3: ' . $dummy->{$access3} . '<br />';
echo 'try direct: ' . $dummy->testArray[0] . '<br />';
The above code will return
try access1: itemNormal
try access2: item1
--- ERROR MESSAGE --
try access3: <-- does not contain anything because of error, my expectation is it will be "item1"
try direct: item1
Is this impossible with php?
$access_handler = explode('[', $access3);
// get array name
$array_name = $access_handler[0];
// get array index
$array_index = substr($access_handler[1], 0, -1);
// call the property
echo $dummy->$array_name[$array_index];
you might create function for this. Otherwise you would have to use eval to evaluate your string, but that is not recommended. Also there are many ways to separate index from array name and then run it as in this example
Related
I want to get value of id and name from the string given below in php. Can any body help me please.
string(35400) "{"cities":[{"id":"3279","name":"Narasaraopet"},{"id":"1852","name":"Srirangapatna"}
That's not really a valid json (missing ]} at the end), but assuming you removed something from it just to post the question, do it like this:
$json = '{"cities":[{"id":"3279","name":"Narasaraopet"},{"id":"1852","name":"Srirangapatna"}]}';
$decoded = json_decode($json, true);
foreach($decoded['cities'] as $v){
echo 'ID: ' . $v['id'] . '<br>';
echo 'Name: ' . $v['name'];
echo '<br><br>';
}
Output:
ID: 3279
Name: Narasaraopet
ID: 1852
Name: Srirangapatna
To use it as objects, you can ommit the second argument, like so:
$decoded = json_decode($json);
Then you'd access the values object-like:
foreach($decoded->cities as $v){
echo 'ID: ' . $v->id . '<br>';
// ...
Read more about json_decode.
Use the function json_decode(), then you'll have an array.
I want to store an property->object name in a table and access in code.
What is the proper way to accomplish the following.
$patient = new stdClass();
$patient->patient_number = '12345';
$cls = 'patient';
$prp = 'patient_number';
echo 'test1 = ' . $patient->patient_number . '<br>';
//--- this prints 12345 as expected;
echo 'test2 = ' . $$cls->patient_number . '<br>';
//--- this print 12345 as expected;
echo 'test3 = ' . $$cls->${$prp} . '<br>';
//--- this generates undefined variable patient_number;
Use this:
echo 'test3 = ' . ${$cls}->{$prp} . '<br>';
I have the following php code:
$skizzar_masonry_item_width = $masonry_item_width;
$skizzar_masonry_item_padding = $masonry_item_padding;
$skizzar_double_width_size = $masonry_item_width*2 +$masonry_item_padding;
$output .= '<style>.skizzar_masonry_entry.skizzar_ma_double, .skizzar_masonry_entry.skizzar_ma_double img {width:'.$skizzar_double_width_size.'}</style>';
return $output;
For some reason though, the value of $skizzar_double_width_size is not being added into the $output - is there a way to echo a value in an output variable?
As #Rizier123 mentioned, ensure you initialise any string variables before trying to append to them.
$var = '';
$var .= 'I appended';
$var .= ' a string!';
I would also like to strongly discourage you from using inline styles as well as generating them with inline PHP. Things get very messy very quickly.
In a situation like this you need to check that all the variables you are using in the calculation are valid before you panic.
So try
echo 'before I use these values they contain<br>';
echo '$masonry_item_width = ' . $masonry_item_width . '<br>';
echo '$masonry_item_padding = ' . $masonry_item_padding . '<br>';
$skizzar_masonry_item_width = $masonry_item_width;
$skizzar_masonry_item_padding = $masonry_item_padding;
$skizzar_double_width_size = $masonry_item_width*2 +$masonry_item_padding;
echo 'after moving the fields to an unnecessary intemediary field<br>';
echo '$skizzar_masonry_item_width = ' . $skizzar_masonry_item_width . '<br>';
echo '$skizzar_masonry_item_padding = ' . $skizzar_masonry_item_padding . '<br>';
echo '$skizzar_double_width_size = ' . $skizzar_double_width_size . '<br>';
$output .= '<style>.skizzar_masonry_entry.skizzar_ma_double, .skizzar_masonry_entry.skizzar_ma_double img {width:'.$skizzar_double_width_size.'}</style>';
echo $output;
This should identify which fields are causing you problems.
Also while testing always run with display_errors = On It saves so much time in the long run.
I'm trying to do something very basic but I can't figure out how.
basically i'm trying to convert the mysql result ($row) into the following format (literal strings):
"0784562627828" => "James",
"0786636363663" => "David",
I have all the data stored in the database and I can get them echoed on my page like so:
$phone = $row['phone'];
$name = $row['name'];
$list .=''.$phone.'';
echo $list;
could someone please advise on this?
Thanks
Just assign them inside an array like you normally would:
$array = array();
while(your fetch here) {
$array[$row['phone']] = $row['name'];
}
To check its contents, you can use var_dump($array) or print_r($array)
Or just straight up show them, like the one you formatted:
while(your fetch here) {
echo '"' . $row['phone'] . '"' . ' => ' . '"' . $row['name'] . '"' . '<br/>';
}
you mean something like this?
$list = array();
$list[$phone] = $name;
Can you do something like
$list = [];
foreach($rows as $row) {
$list[$row['phone']] = $row['name'];
}
I'm using PHPQuery to read some content from HTML, I'm unable to get the element by it's index using the square bracket notation.
See this simple example:
$html = '<div><table id="theTable"><tr><td>FIRST TD</td><td>SECOND TD</td><td>THIRD TD</td></tr></table></div>';
$pq = phpQuery::newDocumentHTML($html);
$table = $pq->find('#theTable');
$tds = $table->find('td');
echo "GETTING BY INDEX:\n\n";
echo '$tds[1] = ' . $tds[1];
echo "\n\n\n";
echo "GETTING IN FOREACH:\n\n";
foreach($tds as $key => $td){
echo '$tds[' . $key . '] = ' . pq($td) . "\n";
}
The output of this is:
GETTING BY INDEX:
$tds[1] =
GETTING IN FOREACH:
$tds[0] = FIRST TD
$tds[1] = SECOND TD
$tds[2] = THIRD TD
I would have expected that I can get the contents of $tds[1] using square brackets, but seems not. How can I get it by index?
Try a var_dump($tds), it'll tell you whats exactly inside the tds. Maybe those keys are actually strings and you should use:
echo "GETTING BY INDEX:\n\n";
echo '$tds['1'] = ' . $tds['1'];
Edit: Also, on your foreach you're using pq(), maybe you should use this
echo "GETTING BY INDEX:\n\n";
echo '$tds[1] = ' . pq($tds[1]);
Found the answer just after posting the question. Instead of square brackets you need to use eq(n):
echo '$tds[1] = ' . $tds->eq(1);
Try the following:
echo '$tds[1] = ' . $tds['1'];