I have Json file which contain alot of Arrays. How can i get the the array Dynamically for each person by name like the get Parameter.
JSON
[{
"Name": "Somename",
"Lastname": "somelastname",
"Address": "someaddress",
},
{
"Name": "Somename1",
"Lastname": "somelastname1",
"Address": "someaddress1",
},
{
"Name": "Somename2",
"Lastname": "somelastname2",
"Address": "someaddress2",
}}
PHP
<?php
error_reporting(0);
$json_file = file_get_contents('jsonfile.json');
$someArray = json_decode($json_file, true);
?>
HTML
<a href="product.php?post=name">
<h3 id="custompage"><?php echo $value["Name"]; ?>
<span><br><?php echo $value["Lastname"]; ?></span>
<span><?php echo $value["Address"]; ?></span>
</h3>
</a>
I don't know what exactly you need to do (find only one match, or fetch all), but this is for both :
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$array = array(
1 => array( "Name" => "Somename1", "Lastname" => "somelastname1", "Address" => "someaddress1"),
2 => array( "Name" => "Somename2", "Lastname" => "somelastname2", "Address" => "someaddress2"),
3 => array( "Name" => "Somename3", "Lastname" => "somelastname3", "Address" => "someaddress3"),
4 => array( "Name" => "Somename4", "Lastname" => "somelastname4", "Address" => "someaddress4")
);
$data1 = json_encode($array);
//var_dump($data1);
$data = json_decode($data1, true);
//var_dump(json_decode($data1));
$myname = "Somename3";
/* one liner to get one result */
if( array_search("$myname", array_column($data, 'Name')) == true ){ echo"[ $myname found ! ]"; } else { echo"[ no data match ! ]"; }
/* loop to get all results */
foreach($data as $user){
echo $user['Name'].' '.$user['Lastname'].' '.$user['Address'].'<br/>';
}
?>
Related
I have two arrays.
The first one is about exchange-rate and the display in my console is like this :
{
"exchange_rate": [
{
"id": "978",
"start_dateTime": "2021-08-01 07:35:02",
"target_value": "1.00000",
"currency_value_euro": "0.84097",
"currency_value_dollar_us": "1.00000",
"id_currency": "1",
"currency": "Dollar am\u00e9ricain",
"currency_symbol": "$US"
},
{
"id": "980",
"start_dateTime": "2021-08-01 07:35:02",
"target_value": "1.00000",
"currency_value_euro": "1.17454",
"currency_value_dollar_us": "0.71600",
"id_currency": "2",
"currency": "Livre sterling",
"currency_symbol": "\u00a3"
}
]
}
These data came from the database and I can display it by choosing particular dates with jQuery.
The second array contains only id_currency which in my console is like this : Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5
On my website, I want to be able to display specific exchange rate by specific currency and dates.
And here my problem appears, I can't find the way to loop on the first array, and loop again inside, on the second array and compare both like if first array has id_currency 1 and second array has id_currency 1 then display the complete line from first array.
I've tried several things but nothing works, at last i've tried this :
foreach ($res as $row){
$idBDD = $row['id_currency'];
$symbolBDD = $row['currency_symbol'];
echo $idBDD;
echo $symbolBDD;
//var_dump($row);
/*foreach ($arr as $line){
$idCheckbox = $line;
echo $idCheckbox;
}
if ($idBDD == $idCheckbox){
echo 'fine';
}
*/
}
I'll be grateful for your help
You need to access the array with $res["exchange_rate"] and loop through it then.
<?php
$res = [
"exchange_rate" => [
[
"id" => "978",
"start_dateTime" => "2021-08-01 07:35:02",
"target_value" => "1.00000",
"currency_value_euro" => "0.84097",
"currency_value_dollar_us" => "1.00000",
"id_currency" => "1",
"currency" => "Dollar américain",
"currency_symbol" => "\$US"
],
[
"id" => "980",
"start_dateTime" => "2021-08-01 07:35:02",
"target_value" => "1.00000",
"currency_value_euro" => "1.17454",
"currency_value_dollar_us" => "0.71600",
"id_currency" => "2",
"currency" => "Livre sterling",
"currency_symbol" => "£"
]
]
];
$output = null;
foreach ($res["exchange_rate"] as $row) {
if (!isset($output)) {
$output = $row;
}
$output = array_intersect_assoc($output, $row);
}
var_dump($output);
I think you could do it like this, of course if you're inside the loop, you won't need to put indices.
read more array_diff()
https://www.php.net/manual/pt_BR/function.array-diff.php
$array = [
"exchange_rate" => [
[
"id" => "978",
"start_dateTime" => "2021-08-01 07:35:02",
"target_value"=> "1.00000",
"currency_value_euro"=> "0.84097",
"currency_value_dollar_us"=> "1.00000",
"id_currency"=> "1",
"currency"=> "Dollar am\u00e9ricain",
"currency_symbol" => "US"
],[
"id"=> "980",
"start_dateTime"=> "2021-08-01 07=> 35=> 02",
"target_value"=> "1.00000",
"currency_value_euro"=> "1.17454",
"currency_value_dollar_us"=> "0.71600",
"id_currency"=> "2",
"currency"=> "Livre sterling",
"currency_symbol"=> "\u00a3"
]
]
];
$array1 = $array['exchange_rate'][0];
$array2 = $array['exchange_rate'][1];
$result = array_diff( $array1, $array2);
var_dump($result);
This question already has answers here:
PHP Append an array to a sub array
(2 answers)
Closed 2 years ago.
I want to achieve below array format:
{
"success": true,
"results": [
{
"name" : "Choice 1",
"value" : "value1",
"text" : "Choice 1"
},
{
"name" : "Choice 2",
"value" : "value2",
"text" : "Choice 2"
}
]
}
However, I am using PHP and a foreach loop, to return some values from my database:
//Search for clients in our database.
$stmt = $dbh->prepare("SELECT * FROM customers");
$stmt->execute();
$showAll = $stmt->fetchAll();
I then have my first part of the array, and my foreach loop:
$data = array(
"success" => false,
"results" => array()
);
foreach ($showAll as $client) {
$data_array[] =
array(
'name' => $client['name'],
'value' => $client['name'],
'text' => $client['name']
);
}
Above only outputs:
[
{
"name":"Choice 1",
"value":"value 1",
"text":"Choice 1"
},
{
"name":"Choice 2",
"value":"value2",
"text":"Choice 2"
}
]
So it is missing the top part of my original array - but I want to loop through each database results in "results": [ ... ]
Try this
$data = array(
"success" => false,
"results" => array()
);
foreach ($showAll as $client) {
$data['results'][] = array(
'name' => $client['name'],
'value' => $client['name'],
'text' => $client['name']
);
}
$data['success'] = true; // if you want to update `status` as well
echo json_encode($data);
After creating $data_array array just add few lines which i have in my post.
Try this code snippet here(with sample input)
ini_set('display_errors', 1);
foreach ($showAll as $client)
{
$data_array[] = array(
'name' => $client['name'],
'value' => $client['name'],
'text' => $client['name']
);
}
// add these lines to your code.
$result=array();
$result["success"]=true;
$result["results"]=$data_array;
echo json_encode($result);
try this as you have an array inside the $data_array on Key "Results" so you should use "results" as a key also and then try to push data in that array
foreach ($showAll as $client) {
$data_array["results"][] =
array(
'name' => $client['name'],
'value' => $client['name'],
'text' => $client['name']
);
}
You can simply use json_encode and push it to your result array
$data = array(
"success" => false,
"results" => array()
);
$result = [
[
"name" => "Choice 1",
"value" => "value 1",
"text" => "Choice 1"
],
[
"name" => "Choice 2",
"value" => "value2",
"text" => "Choice 2"
]
];
$data['results'] = json_encode($result);
I want to adjust my json a tiny bit by adding an additional message, I want to reach, in this example, an image and all of its things inside the name that I makeup ("GetImage" in this case). I will give you an example so you can understand it easier.
This is what I want it to look like:
{
"results": [{
"GetImage": //this is what I am missing
{
"ID": "File",
"Name": "image3",
"URL": "http://test.image.com/
}
}
]}
I have:
{
"results": [
{
"ID": 1,
"Name": "Aqua",
"URL": "http://www.img.com/img1.jpg"
}
]}
So I have to add a text named GetImage, so I can find that particular Image and its info but I am not sure where to put it in my current code:
This is my code:
<?php
$contacts = array();
$contacts[] = array("ID"=>1, "Name" => 'Aqua', "URL"=>'http://www.img.com/img1.jpg');
$contacts[] = array("ID"=>2, "Name" => 'Vit', "URL"=>'http://www.img.com/img2.jpg');
$contacts[] = array("ID"=>3, "Name" => 'Sit', "URL"=>'http://www.img.com/img3.jpg');
echo json_encode(array('results' => $contacts), JSON_PRETTY_PRINT);
?>
This Code Can Do The Job:
<?php
$contacts = array();
$contacts[] = array("ID"=>1, "Name" => 'Aqua', "URL"=>'http://www.img.com/img1.jpg');
$contacts[] = array("ID"=>2, "Name" => 'Vit', "URL"=>'http://www.img.com/img2.jpg');
$contacts[] = array("ID"=>3, "Name" => 'Sit', "URL"=>'http://www.img.com/img3.jpg');
$results = array();
$results["results"] = array('GetImage' => $contacts);
echo json_encode($results, JSON_PRETTY_PRINT);
?>
Try this code:
<?php
$contacts = array();
$contacts[] = array('GetImage' => array("ID"=>1, "Name" => 'Aqua', "URL"=>'http://www.img.com/img1.jpg'));
$contacts[] = array('GetImage' => array("ID"=>2, "Name" => 'Vit', "URL"=>'http://www.img.com/img2.jpg'));
$contacts[] = array('GetImage' => array("ID"=>3, "Name" => 'Sit', "URL"=>'http://www.img.com/img3.jpg'));
echo json_encode(array('results' => $contacts), JSON_PRETTY_PRINT);
?>
How would I structure $array so that when I pass it to json_encode
$output = json_encode($array);
the output would be:
$output = [
{apples:"33" ,oranges:"22"},
{apples:"44" ,oranges:"11"},
{apples:"55" ,oranges:"66"},
]
Are there any options I need to use to get the output I need? Or is it all about how I structure my PHP array?
This should work for you:
[] = array
{} = object
key:value = key : value
<?php
$array = [
(object)["apples" => "33", "oranges" => "22"],
(object)["apples" => "44", "oranges" => "11"],
(object)["apples" => "55", "oranges" => "66"],
];
echo $output = json_encode($array);
?>
Output:
[
{
"apples": "33",
"oranges": "22"
},
{
"apples": "44",
"oranges": "11"
},
{
"apples": "55",
"oranges": "66"
}
]
You will just need to pass an array of associative arrays to json_encode
$array = array (
array(
'apples'=>'33',
'oranges'=>'22'
),
array(
'apples'=>'44',
'oranges'=>'11'
),
array(
'apples'=>'55',
'oranges'=>'66'
)
);
you can pass with an array of associative arrays in json_encode($var).
$array = array (
array(
'johan'=>'male',
'age'=>'22'
),
array(
'lucy'=>'female',
'age'=>'24'
),
array(
'donald'=>'male',
'age'=>'28'
)
);
I'm trying search process input with php and send to json
the json output like this:
{
"err": 0,
"msg": "",
"data": {
"f": 0,
"hotel": [
{
"att": 25147,
"name": "Crowne Plaza Changi Airport",
"city": "Singapore",
"country": "Singapore"
}
],
"city": [
{
"att": "-2679652",
"name": "Singapore",
"region": "",
"country": "Singapore",
"nr_hotels": ""
}
]
}
}
i was trying with test array but not work
<?
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);
// Do Search
$json_array = array();
while ($search=mysql_fetch_array($result)){
$search_array = array(
"err"=>"","msg"=>"",
"hotel" => array(
"att" => $search['hotel_id'],
"name" => $search['hotel_name'],
"city" => $search['city'],
"country" => $search['country']),
"city"=> array(
"att"=> $search['hotel_id'],
"name" => $search['city'],
"country" => $search['country'])
);
array_push($json_array,$search_array);
}
echo json_encode($json_array);
?>
i change with this , but show only 1 record not all similar from key search
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);
// Do Search
$json_array = array();
while ($hasil=mysql_fetch_array($result)){
$search_array = array(
"err"=>intval("0"),"msg"=>"","data"=>array("f"=>intval("114"),
"hotel"=>array(
$hotel_array = array(
"att"=> $hasil['hotel_id'],
"name" => $hasil['hotel_name'],
"city" => $hasil['city'],
"country" => $hasil['country']
)),
"city"=>array(
$city_array = array(
"att"=> $hasil['hotel_id'],
"name" => $hasil['city'],
"city" => $hasil['city'],
))
));
}
echo json_encode($search_array);
?>
please help how the syntax for array thank
it's similar process with this site http://www.myhotelfinder.com/id/home/dohttp/predict?q=singapore
thank you this json output what i mean
<?
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);
// Do Search
$json_array = array(
"err" => "",
"msg" => "",
"data" => array(
"f" => 0,
"hotel" => array(),
"city" => array()
)
);
while ($hasil=mysql_fetch_array($result)){
$hotel = array(
"att" => $hasil['hotel_id'],
"name" => $hasil['hotel_name'],
"city" => $hasil['city'],
"country" => $hasil['country']
);
$city = array(
"att"=> $hasil['hotel_id'],
"name" => $hasil['city'],
"country" => $hasil['country']
);
array_push($json_array["data"]["hotel"], $hotel);
array_push($json_array["data"]["city"], $city);
}
echo json_encode($json_array);
?>
kiss ^^
I think you are asking how you need to format your array within the while block in order to achieve the JSON output.
I think you could try the following array declaration:
<?
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);
// Do Search
$json_array = array();
while ($search=mysql_fetch_array($result)){
$search_array = array(
"err" => "",
"msg" => "",
"data" => array(
"f" => 0,
"hotel" => array(
"att" => $search['hotel_id'],
"name" => $search['hotel_name'],
"city" => $search['city'],
"country" => $search['country']
),
"city" => array(
"att"=> $search['hotel_id'],
"name" => $search['city'],
"country" => $search['country']
)
)
);
array_push($json_array,$search_array);
}
echo json_encode($json_array);
?>
UPDATE:
Ok, I am starting to see a but more what you might be doing. Try the following:
<?
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);
// Do Search
$json_array = array(
"err" => "",
"msg" => "",
"data" => array(
"f" => 0,
"hotel" => array(),
"city" => array()
)
);
while ($hasil=mysql_fetch_array($result)){
$hotel = array(
"att" => $hasil['hotel_id'],
"name" => $hasil['hotel_name'],
"city" => $hasil['city'],
"country" => $hasil['country']
);
$city = array(
"att"=> $hasil['hotel_id'],
"name" => $hasil['city'],
"country" => $hasil['country']
);
array_push($json_array["data"]["hotel"], $hotel);
array_push($json_array["data"]["city"], $city);
}
echo json_encode($json_array);
?>