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"
}
Related
I can't manage to sort an array alfabetically.
It's an array with cities that I get from an external XML.
The XML looks like this, and it's the node localidad I am trying to sort.
<parada>
<id>506</id>
<localidad>
<![CDATA[ Alvor ]]>
</localidad>
<parada>
<![CDATA[ Alvor Baia Hotel (Bus Stop Alvor Férias) ]]>
</parada>
<lat>37.1296</lat>
<lng>-8.58058</lng>
<horasalida>05:40</horasalida>
</parada>
The relevant code:
$xml = new SimpleXMLElement($viajes);
foreach ($xml->parada as $excursion) {
$newParadasarray[] = $excursion->localidad;
}
$newParadasarray = array_unique($newParadasarray);
foreach ($newParadasarray as $parada) {
if (strpos($parada, 'Almuñecar') !== false)
echo '<option value="Almuñecar">Almuñecar</option>';
if (strpos($parada, 'Benalmádena') !== false)
echo '<option value="Benalmádena Costa">Benalmádena Costa</option>';
if (strpos($parada, 'Estepona') !== false)
echo '<option value="Estepona">Estepona</option>';
etc.
}
I have tried with sort() and array_values().
This is the output of print_r($newParadasarray):
Array (
[0] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
[1] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
[2] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
[4] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
[9] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
[14] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
[20] => etc.
The problem is that your assigning a SimpleXMLElement into the array, instead you want the content of the element, so just change the line...
$newParadasarray[] = $excursion->localidad;
to
$newParadasarray[] = trim((string)$excursion->localidad);
The cast (string) takes the text content and trim() removes the extra whitespace around it.
I am assuming that you have multiple <parada> elements, so that $xml->parada is returning the correct data.
If you're familiar with DOMDocument you could simply do this:
$doc = new DOMDocument();
$doc->loadXML($xml);
$array = array();
foreach($doc->getElementsByTagName("localidad") as $localidad) {
$array[] = trim($localidad->nodeValue);
}
$array = array_unique($array);
sort($array);
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)");
}
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);
}
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($..)
I'm close to finishing a script which uses simplexml to process some data but I am stuck at the last.
Well, not stuck, but curious and stuck.
$tmp contains my XML (approx 750k) and the goal is to get it into a format for fputcsv (a parent array containing an array for each row).
The below works, but instead of a nice clean array that I can use fputcsv and send back to user there is a lot of overhead which I do not need for writing to a CSV file and which will create me problems no doubt with fputcsv. I have cleaned the output up into just a couple of header columns and just a couple of rows, but in reality there are 20 columns and hundreds of rows. Anyway to clean before fputcsv?
// Process XML response into arrays for fputcsv
$csv = array();
$xml = simplexml_load_string($tmp);
// Create array and loop each column for header row
$header_row = array();
foreach ($xml->RESPONSE->DATA->HEADER->COLUMN as $column) {
$header_row[] = $column;
}
$csv[] = $header_row;
// Create array and loop each column for header row
foreach ($xml->RESPONSE->DATA->ROW as $row) {
$data_row = array();
foreach ($row->COLUMN as $datacolvalue){
$data_row[] = $datacolvalue;
}
$csv[] = $data_row;
unset($data_row);
}
// Output var to check for accuracy (later add fputcsv)
var_dump($csv);
Output (print_r - can var_dump if required):
Array
(
[0] => Array
(
[0] => SimpleXMLElement Object
(
[0] => date
)
[1] => SimpleXMLElement Object
(
[0] => name
)
)
[1] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[data_type] => text
)
[0] => Aug 01, 2011
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[data_type] => text
[id] => 8699636
)
[0] => bfxgbfgxbfbgxfsfsdf
)
)
[2] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[data_type] => text
)
[0] => Aug 01, 2011
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[data_type] => text
[id] => 8699694
)
[0] => bfxgbfgxbfbgxf
)
)
)
Desired output (extrapolated for each additional column and removing #attributes):
array(
array("date","name"),
array("Aug 01, 2011","bfxgbfgxbfbgxfsfsdf"),
array("Aug 01, 2011","bfxgbfgxbfbgxf")
)
Try changing
foreach ($xml->RESPONSE->DATA->HEADER->COLUMN as $column) {
to
$columns = (array)$xml->RESPONSE->DATA->HEADER->COLUMN;
foreach ($columns as $column) {
That should convert the SimpleXML objects to simple arrays. Which should remove the "overhead". Do the same for foreach ($row->COLUMN as $datacolvalue){.
Answering my own question, because I probably didn't explain it well enough, but specifying a (string) got me to where I needed to be.