I'm having some problems getting the array in these objects. When I print_r(), the following code is printed. $message_object is the name of the object.
SimpleXMLElement Object
(
[header] => SimpleXMLElement Object
(
[responsetime] => 2012-12-22T14:10:09+00:00
)
[data] => SimpleXMLElement Object
(
[id] => Array
(
[0] => 65233
[1] => 65234
)
[account] => Array
(
[0] => 20992
[1] => 20992
)
[shortcode] => Array
(
[0] => 3255
[1] => 3255
)
[received] => Array
(
[0] => 2012-12-22T11:04:30+00:00
[1] => 2012-12-22T11:31:08+00:00
)
[from] => Array
(
[0] => 6121843347
[1] => 6121820166
)
[cnt] => Array
(
[0] => 24
[1] => 25
)
[message] => Array
(
[0] => Go tramping wellington 11-30
[1] => Go drinking Matakana 2pm
)
)
)
I'm trying to get the id arrays out of the objects with a foreach:
foreach($message_object->data->id AS $id) {
print_r($id);
}
The following reply is sent:
SimpleXMLElement Object ( [0] => 65233 ) SimpleXMLElement Object ( [0] => 65234 )
How do I get the value of [0] or am I going about this wrong? and is there a way to loop though the results and get the object keys?
I have tried to echo $id[0] but it returns no result.
When you use print_r on a SimpleXMLElement there comes magic in between. So what you see is not actually what is there. It's informative, but just not the same as with normal objects or arrays.
To answer your question how to iterate:
foreach ($message_object->data->id as $id)
{
echo $id, "\n";
}
to answer how to convert those into an array:
$ids = iterator_to_array($message_object->data->id, 0);
As this would still give you the SimpleXMLElements but you might want to have the values you can either cast each of these elements to string on use, e.g.:
echo (string) $ids[1]; # output second id 65234
or convert the whole array into strings:
$ids = array_map('strval', iterator_to_array($message_object->data->id, 0));
or alternatively into integers:
$ids = array_map('intval', iterator_to_array($message_object->data->id, 0));
You can cast the SimpleXMLElement object like so:
foreach ($message_object->data->id AS $id) {
echo (string)$id, PHP_EOL;
echo (int)$id, PHP_EOL; // should work too
// hakre told me that this will work too ;-)
echo $id, PHP_EOL;
}
Or cast the whole thing:
$ids = array_map('intval', $message_object->data->id);
print_r($ids);
Update
Okay, the array_map code just above doesn't really work because it's not strictly an array, you should apply iterator_to_array($message_object->data_id, false) first:
$ids = array_map('intval', iterator_to_array$message_object->data->id, false));
See also: #hakre's answer.
You just need to update your foreach like this:
foreach($message_object->data->id as $key => $value) {
print_r($value);
}
Related
I have to access to all values stored in a big multidimensional array, here's an example of print_r the array:
Array
(
[Novedad] => Array
(
[#attributes] => Array
(
[CUNENov] => 4545454545
)
)
[Periodo] => Array
(
[#attributes] => Array
(
[FechaIngreso] => 1998-12-12
[FechaRetiro] => 2021-11-12
[FechaLiquidacionInicio] => 2021-05-01
[FechaLiquidacionFin] => 2021-05-30
[TiempoLaborado] => 10829
[FechaGen] => 2021-05-05
)
)
[Devengados] => Array
(
[Basico] => Array
(
[#attributes] => Array
(
[DiasTrabajados] => 30
[SueldoTrabajado] => 1258955.00
)
)
)
)
The thing I want to do is extract the values from that array, I have tried this way:
<?php
$cunenov = $array['Novedad']['#attributes']['CUNENov'];
but doesn't work..
Any suggests?. Thanks in advance.
I wanted you to see the "constructed" array and the outputs .. Stand alone, this php works .. Paste it into a stand alone php file and determine what you're doing differently to not achieve the same result.
Building out the array, and then print_r so you can validate it's the same structure as your array.
<?php
$test = Array();
$test['Novedad'] = array();
$test['Novedad']['#attributes'] = array();
$test['Novedad']['#attributes']['CUNENov'] = 4545454545;
print_r( $test );
Yields:
Array
(
[Novedad] => Array
(
[#attributes] => Array
(
[CUNENov] => 4545454545
)
)
)
Then we echo:
$cunenov = $test['Novedad']['#attributes']['CUNENov'];
echo "Value is $cunenov";
Yields
Value is 4545454545
I have JSON like this (from nested sorting)
[
{"id":13},{"id":14},
{"id":15,
"children":[
{"id":16},{"id":17},{"id":18}
]
},
{"id":19},{"id":20},
{"id":21,
"children":[
{"id":22}
]
}
]
how I PHP loop to put this JSON in MySQL
Thank you.
As with any valid JSON format string, you can use PHP's built-in json_decode to convert it into a parsable object and then loop through those parameters. In this case, from the JSON string you've given (which seems to be an array)
$array = json_decode($string);
foreach ($array as $val) {
//The object $val will be:
"id":13
}
If it's nested, you would do another foreach loop and detect for the property that needs to be looped. For example you could do this in a variety of ways (check for the "children" property, loop through the $val properties and check if it is an array, if it is then loop through that). Inside this foreach loop iteration, you can insert it, or do execute whatever statement you need to get it inside MySQL
Your question is pretty vague on the format and way you want it inserted. I'd suggest showing some code you've tried so other people can help you and know what direction you're going in. (that's probably the reason for the downvote, not mine by the way)
Looping through all the properties of object php
just put your valid json inside json_decode and assign it to a php array as below
//$php_arr = json_decode('YOUR_JSON');
$php_arr = json_decode('[{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]},{"id":19},{"id":20},{"id":21,"children":[{"id":22}]}]');
/*comment the following 3 lines when done*/
echo "<pre>";
print_r($php_arr);
echo "</pre>";
/*comment the above 3 lines when done*/
output
Array
(
[0] => stdClass Object
(
[id] => 13
)
[1] => stdClass Object
(
[id] => 14
)
[2] => stdClass Object
(
[id] => 15
[children] => Array
(
[0] => stdClass Object
(
[id] => 16
)
[1] => stdClass Object
(
[id] => 17
)
[2] => stdClass Object
(
[id] => 18
)
)
)
[3] => stdClass Object
(
[id] => 19
)
[4] => stdClass Object
(
[id] => 20
)
[5] => stdClass Object
(
[id] => 21
[children] => Array
(
[0] => stdClass Object
(
[id] => 22
)
)
)
)
Now as you have PHP array do what ever you want like
foreach($php_arr as $arr){
if(!isset($arr['children'])){
$q = "insert into tbl(id) values('".$arr['id']."')";
}else{
//your logic
}
}
You need to use json_decode function to decode JSON data. Then use foreach loop to manipulate data.
Try example
$str = '
[{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]},{"id":19},{"id":20},{"id":21,"children":[{"id":22}]}]
';
$json = json_decode($str);
//var_dump($json);
foreach ($json as $item)
{
//The object $val will be:
echo $item->id."<br />";
//You INSERT query is here
//echo "INSERT INTO table (field) VALUE ($item->id)";
$query = mysqli_query($conn, "INSERT INTO table (field) VALUE ($val->id)");
}
Given a deep array, how do you display line-delimited values from it?
Array (
[0] => Array (
[godzina] => SimpleXMLElement Object (
[0] => 17:00:00
)
[data] => SimpleXMLElement Object (
[0] => 2015-09-02
)
[kurs] => SimpleXMLElement Object (
[0] => 2.03
)
)
)
This is the output I'm expecting:
hours: 17:00:00
date: 2015-09-02
exchange: 2.03
I get this array from an xml, via curl, so the source can't be altered.
Figured Out:
$h = 'godzina';
$hvalue = $zmt[$h];
$d = 'data';
$dvalue = $zmt[$d];
$e = 'kurs';
$evalue = $zmt[$e];
and then echo with [0].
BUT thanks. For foreach loop.
Assuming your array is in a variable $myArr, you should be able to do a simple foreach:
foreach ($myArr[0] as $k => $v) {
echo $k.": ".$v[0]."\n"
}
I'm trying to check if there is a duplicate value in a foreach loop.
Here's my attempt which ain't working:
$popup_array = array();
foreach($xml->config->popup as $popup_item)
{
$duplicate_test = $popup_item->attributes()->name;
if (in_array_r($duplicate_test, $popup_array)){
echo "match found for " . $duplicate_test;
}
echo "item: " . $duplicate_test . "<br />";
$popup_array[$i] = $duplicate_test;
$i++;
}
Now i can clearly see there is 2 duplicates, here is what i see at end when i print_r as you can see 2 x default and 2 x lost and the echo also shows default and lost so the in_array is not working and im not sure why:
[0] => SimpleXMLElement Object
(
[0] => Default
)
[1] => SimpleXMLElement Object
(
[0] => Default
)
[2] => SimpleXMLElement Object
(
[0] => pipe
)
[3] => SimpleXMLElement Object
(
[0] => raised
)
[4] => SimpleXMLElement Object
(
[0] => steal
)
[5] => SimpleXMLElement Object
(
[0] => lost
)
[6] => SimpleXMLElement Object
(
[0] => lost
)
[7] => SimpleXMLElement Object
(
[0] => teach
)
[8] => SimpleXMLElement Object
(
[0] => terrain
)
Is there an error in my code? Is it something to do with the simpleXMLEelement and thats turned it into a multidimensional array and i need to search a different way. If i loop through the array and do this:
$popup_length = count($popup_array);
for($x=0;$x<$popup_length;$x++)
{
echo $popup_array[$x];
echo "<br>";
}
It returns:
Default
Default
pipe
raised
steal
lost
lost
teach
terrain
I think, it should be like this
$popup_array = array();
foreach($xml->config->popup as $popup_item)
{
$duplicate_test = (string) $popup_item->attributes()->name;
if (!in_array($duplicate_test, $popup_array)){
$popup_array[] = $duplicate_test;
}
else {
echo "match found for " . $duplicate_test;
}
}
You should check if not in array and then push/add it inside $popup_array, no need to use $i as an index for the array. Also check SimpleXMLElement::attributes.
This value returns an object, not a string:
$popup_item->attributes()->name
So, the objects probably differ by some XML attribute other than name. Try casting to a string, so that your array index is just the name:
$duplicate_test = (string) $popup_item->attributes()->name;
use array_diff
$arr = array(1=>'word',2=>'otherword',3 =>'Hello' ,4=>'hello', 5=>'KKKKK');
//Case Sensitive
$withoutDuplicates = array_unique(array_map("strtoupper", $arr));
$duplicates = array_diff($arr, $withoutDuplicates);
print_r($duplicates);
will print :
Array
(
[3] => Hello
[4] => hello
)
I have this code:
$id = new matrix(array(0=>array(1,0.5,3), 1=>array(2,1,4), 2=>array(1/3,1/4,1)));
$soma = $id->times($id)->sumRows();
That outputs this:
matrix Object ( [numbers] => Array ( [0] => Array ( [0] => 12.75 [1] => 22.3333333333 [2] => 4.83333333333 ) ) [numColumns] => 3 [numRows] => 1 )
and:
$total = $id->times($id)->sumRows()->sumTotal($id);
That outputs this:
matrix Object ( [numbers] => Array ( [0] => Array ( [0] => 39.9166666667 ) ) [numColumns] => 3 [numRows] => 1 )
Now, i am trying to make:
foreach ($soma as $value){
$final = (int)$value/(int)$total;
print_r ((int)$final);
}
The output will be 000.
Must be:
12.75/39.9166666667 = 0,3269230769230769
22.3333333333 / 39.9166666667 = ...
and so on
Thanks!
Just some ideas, without really knowing much about the matrix class...
All those (int)s should probably be (float)s, as you seem to want a non-int answer.
$value is itself an object, so you'll probably need to use $value['numbers'][0][0 or 1 or 2]. Same goes for $total.
the issue is solved :
documentation:
get_data($..)