So I'm trying to find the details for a particular item in one array from a different array:
foreach($json_items['result']['items'] as $item)
{
foreach($items_all['items_game']['items'] as $schemaItem)
{
echo $Schema[$item['defindex']];
if($item['defindex'] == $Schema[$item['defindex']])
{
echo "works";
echo $schemaItem['name'].'<br />';
break;
} else {
//echo "not";
}
}
}
defindex is a unqiue ID for the item, Schema is a database type array of item info
but Schema is designed like this:
[1] => Array
(
[name] => Anti-Mage's Glaive
[prefab] => default_item
So 1 here would be the defindex for this item in the Schema array
What can I do so can compare them and get out the info such as name and prefab for the item? Thanks.
Is this inside of a loop through the array? If so, you could try:
foreach ($item as $key => $value){
// Do compare here, $key would be the array index $value the value.
}
You could access the array in $Schema
$item = $Schema[$item['defindex']];
$item_name = $item['name'];
$item_prefab = $item['prefab'];
Related
So not a stranger to PHP or arrays even, but never had to deal with multidimensional arrays and its doing my head in.
I have the output of a PHP to a server API and need to pull all the mac address values from the (dst_mac) keys, but only on the occasion the category (catname) keys value for each element is emerging-p2p
The format of the array is like this (intermediate keys and values removed for brevity)
[1] => stdClass Object
(
[_id] => 5c8ed5b2b2302604a9b9c78a
[dst_mac] => 78:8a:20:47:60:1d
[srcipGeo] =>
[dstipGeo] => stdClass Object
(
)
[usgipGeo] => stdClass Object
(
)
[catname] => emerging-p2p
)
Any help much appreciated, i know when im out of my depth!
From your example that is an array with a std class. you can use the empty funtion.
//checks if the first key
if (!empty($array[1]->_id)) {
echo $array[1]->dst_mac;
// or do what you want.
}
This example only applies to one array. use a loop to have this dynamically done.
EDIT: My answer was based on your question. Didn't realize you have to check the catname to be 'emerging-p2p' before you get the mac address?
// loop through the array
foreach ($array as $item) {
// checks for the catname
if ($item->catname === 'emerging-p2p') {
// do what you want if the cat name is correct
echo $item->dst_mac;
}
}
Is this what you want?
for($i =0;$i<count($arr);$i++){
if(isset($arr[$i]['catname']) && $arr[$i]['catname']=='emerging-p2p'){
echo $arr[$i]['dst_mac'];
}
}
If there is only one object that has 'emerging-p2p' cat name:
foreach ($your_list_of_objects as $obj) {
if ($obj->catname == 'emerging-p2p') {
return $obj->dst_mac;
}
}
If there are many:
$result = [];
foreach ($your_list_of_objects as $obj) {
if ($obj->catname == 'emerging-p2p') {
$result[]= $obj->dst_mac;
}
}
return $result;
Use for loop.
for($i =0; $i<=count($arr); $i++){
if(arr[$i]['catname']=='emerging-p2p'){
echo arr[$i]['dst_mac'];
}
}
To fetch all the mac where catname is emerging-p2p
//Assuming $arr has array of objects
$result_array = array_filter($arr, function($obj){
if ($obj->catname == 'emerging-p2p') {
return true;
}
return false;
});
foreach ($result_array as $value) {
echo $value->dst_mac;
}
You can use array_column if you need only mac address on the basis of catname.
$arr = json_decode(json_encode($arr),true); // to cast it as array
$temp = array_column($arr, 'dst_mac', 'catname');
echo $temp['emerging-p2p'];
Working demo.
how can i update data from array multidimension in php, but its from key and value array like this :
$array1=array(
array('data1'=>'name','data2'=>'age'),
array('data1'=>'names','data2'=>'ages')
);
how can i get result from array like this
UPDATE tablename SET data1='name',data2='age' WHERE 1;
i tried this but the result not like above
foreach($array1 as $arrays) {
foreach($arrays as $key => $value){
echo $key."="."'".$value."'".", ";
}
}
result:
data1='name',data2='age', data1='name',data2='age'
i want result like this :
data1='name',data2='age'
i hope can help me.
Because you only want to get content of first array, you should loop through first array only
foreach($array1[0] as $key => $value){
echo $key."="."'".$value."'".", ";
}
// data1='name', data2='age',
I have a data set being returned from a database and the keys are the names of the fields I need to print on a table.
For example, if I have a array of 5 keys (fields), I need to create an HTML table that had a heading with each of the keys on it. I would then need to print the results to the table.
Esentially, I need to create an HTML table from an array and the keys in the first result can act as the headers.
Here is my data:
SimpleXMLElement Object
(
[data] => SimpleXMLElement Object
(
[primaryFields] => SimpleXMLElement Object
(
[primary] => Array
(
[0] => SimpleXMLElement Object
(
[Project_Title] => Test
[Project_Description_Preview] => Test Desc
)
[1] => SimpleXMLElement Object
(
[Project_Title] => Blueprint Development Project
[Project_Description_Preview] => We are continuing to improve Blueprint as a tool by collecting feedback from our internal users and developing new features.
)
What would be the best way to go about getting the keys from the first result in the array? Should this be two separate loops; first one loops to create the headers from the keys and then the second one prints the data?
Project_Title & Project_Description_Preview would be the table headers in this case.
I will leave it to you to parse the data you get back. From the look of the object get to primary.
<?php
//You will need to parse the object to get this.
$primary = array(
array(
"Project_Title" => "Test",
"Project_Description_Preview" => "Test Desc"
),
array(
"Project_Title" => "Blueprint Development Project",
"Project_Description_Preview" => "We are continuing to improve Blueprint as a tool by collecting feedback from our internal users and developing new features."
)
);
echo "<table>";
for($i = 0; $i < count($primary); $i++){
//Only create the head on your first object
if($i==0){
echo "<thead>";
//build header here
echo "<tr>";
foreach($primary[$i] as $key => $value){
echo "<th>" . $key . "</th>";
}
echo "</tr>";
echo "</thead><tbody>";
}
//Then create the rows and data like all php...
echo "<tr>";
foreach($primary[$i] as $value){
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</tbody></table>";
?>
you have to browse your array from a certain place, say
$arr->data->primaryFields->primary as $primary.
considering the array from primary.
foreach($primary as $values){
foreach($values as $key => $ value){
echo $key." & ".$value;
}
}
My typical approach would be just to grab the keys when you first descend into the data on your first iteration, assuming you're looping over it.
Looping like so (as a bonus lets extract the keys just in case):
$primary = $obj->data->primaryFields->primary; //an array
$first = true;
$keys = array();
foreach ($primary as $obj) {
if ($first) {
$first = false;
$keys = get_array_keys((array)$obj));
make_headers($obj,$keys); //do header things here
}
make_table_stuff($obj,$keys); //do table stuff
}
But, you could always do:
$keys = get_object_vars($obj->data->primaryFields->primary[0]);
Note: this will work for any number of database columns, whether they are known or not.
You should be able to build the whole table like this:
//where $obj = your output...
//get the column names, convert to an object and store inside an array of one element
$keys = array(json_decode(json_encode(array_keys(get_object_vars($obj[0])))));
//create the output array by merging the keys as the first element with the existing dataset into a new array
$output = array_merge($keys, $obj);
//here is the output step
echo "<table><thead>";
foreach($output as $record_num => $record) {
echo '<tr>';
$cells = get_object_vars($record);
foreach ($cells as $column => $cell) {
echo '<td class="' . $column . '">' . $cell . '</td>';
}
echo '</tr>';
//if this is the first element of the array, it is the column names. After these are output, the rest should be in the tbody tag
if ($record_num == 0) { echo '</thead><tbody>';}
}
echo "</tbody></table>";
This also adds a class name to each cell that corresponds to its database column, which can be used for vertical css styling or javascript selectors after the fact for UI considerations. This can be removed if not needed though.
I have custom fields that are created dynamicaly. I get the data from those fields and store it into a database as an array with update_post_meta. It's stored as a serialised array in the database:
a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}
Now I need to get this array and echo it out on the website, so it looks something like: 4 children (1993,1994,1995,1996).
Here's the code I use now, but it doesn't work.
<?php
$children = get_post_custom_values('rbchildyear');
foreach ($children as $key => $value){
echo "$key => $value('rbchildyear')<br>";
}
?>
And thats what I get in the front office:
0 => a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}('rbchildyear')
So how can I do that?
Thank you!
use unserialize().
$children = unserialize('a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}');
print_r($children);
This will return array
If you use get_post_meta it will return an array of values (numerically indexed). Then you can loop through the array with a foreach.
$childYears = get_post_meta($post_id, "rbchildyear", true);
foreach($childYears AS $theYear)
{
$printThis .= $theYear.",";
}
print count($childYears)." children ( ".$printThis." )";
I have an array in the database. When I used print_r($variable_name), I got the array like this
Array
(
[0] => Array
(
[attribute_name] => Disk space,Color,Processor
)
)
So to get the value of attribute_name I tried this
foreach($attributes_name as $key=>$val) {
echo $val['attribute_name'];
}
Here I got the result like Disk space,Color,Processor. But I want the result should come like a list
<li>Disk Space</li>
<li>Color</li>
<li>Processor</li>
So can someone tell me how to do this?
Try this :
<?php
$arr = array(array("attribute_name" => "Disk space,Color,Processor"));
foreach($arr as $val) {
$resultArr = explode(",",$val['attribute_name']);
foreach($resultArr as $value){
echo "<li>".$value."</li>";
// Add id in li
echo "<li id='".str_replace(" ","_", strtolower($value))."'>".$value."</li>";
}
}
?>