I need to be able to get the data out of the following functions:
getStuff();
getRelateStuff();
getRelatedStuffInStuff();
Begin Code:
function getStuff()
{
return array("It", "Works");
}
function Stuff()
{
$value = array(getStuff());
foreach ($value as $key => $value)
{
echo "$key $value <br />\n";
}
}
function getRelatedStuff()
{
return array("hello" => "world", "cake"=> "is a lie");
}
function RelatedStuff()
{
$value = array(getRelatedStuff());
foreach ($value as $key => $value)
{
echo "$key $value <br />\n";
}
}
//Related in Stuff
function getRelatedStuffInStuff()
{
$s1 = array("hello" => "world", "cake"=> "is a lie");
$s2 = array("apple" => "mac", "microsoft"=> "windows", "linus" => "linux");
$s3 = array("OSX" => "10.6", "Ubuntu" => "11.04", "Windows" => "7");
return array($s1, $s2, $s3);
}
function RelatedInStuff()
{
$value = array(getRelatedStuffInStuff());
foreach ($value as $key => $value)
{
echo "$key $value <br />\n";
}
}
?>
When I try to view this page in a web browser it just shows a blank page. I am doing anything correctly? What should or what can I change?
If this is your full code... you have to call the functions.
Also your functions already return an array so you don't need to wrap those calls in array(...).
Try to add this at the end of your code (the ?> is the last line in what you posted):
?>
<html><body>
<?php
RelatedInStuff();
?>
</body></html>
This will actually generate some HTML and call one of your functions.
Related
I want to select the "RSI" from the first element only.
Array (json file):
{
"Technical": {
"2019-01-11 15:30": {
"RSI": "123"
},
"2019-01-11 14:30": {
"RSI": "456"
}
"2019-01-11 14:30": {
"RSI": "789"
}
}
My php:
foreach ($json['Technical'] as $field => $value) {
echo $value['RSI']; // Gives 123456789
}
I want only 123
I tried:
echo $value[0]['RSI']; // Gives NULL
Break the loop with break; and it will only return the first item.
foreach ($json['Technical'] as $field => $value) {
echo $value['RSI']; // Gives 123
break;
}
If you want specific items then use a "$key" variable.
$key = 0;
foreach ($json['Technical'] as $field => $value) {
if($key == 0 || $key ==1){
echo $value['RSI'];
}
$key++;
}
// 123456
Change the if to suit your needs.
I am decoding the array of json objects into html lists . i have tried with some demo it worked but when i deal with this array of json this gives error as : Invalid argument supplied for foreach(). what is i am missing ?
<?php
$json_string = '{"error":false,"data":[{"jb_product_category_id":"1","jb_product_category_name":"Mother","jb_product_category_prefix":"jbpm","jb_product_category_delete_status":"0","jb_product_category_created_on":"1501500876531","jb_product_category_updated_on":"1501500876531","subCategory1":[{"jb_product_subcategory1_1_id":"1","jb_product_subcategory1_2_category_id":"1","jb_product_subcategory1_3_name":"Cloths","jb_product_subcategory1_4_delete_status":"0","jb_product_subcategory1_5_created_on":"1501563015164","jb_product_subcategory1_6_updated_on":"1501563015164","subCategory2":[{"jb_product_subcategory2_1_id":"1","jb_product_subcategory2_2_category_id":"1","jb_product_subcategory2_3_subcategory1_id":"1","jb_product_subcategory2_4_name":"Pregnancy wear","jb_product_subcategory2_5_delete_status":"0","jb_product_subcategory2_6_created_on":"1501574226464","jb_product_subcategory2_7_updated_on":"1501574226464"}]}]},{"jb_product_category_id":"2","jb_product_category_name":"Child Wear","jb_product_category_prefix":"jbpc","jb_product_category_delete_status":"0","jb_product_category_created_on":"1502429483534","jb_product_category_updated_on":"1502429483534","subCategory1":[{"jb_product_subcategory1_1_id":"2","jb_product_subcategory1_2_category_id":"2","jb_product_subcategory1_3_name":"Girls","jb_product_subcategory1_4_delete_status":"0","jb_product_subcategory1_5_created_on":"1502429606169","jb_product_subcategory1_6_updated_on":"1502429606169","subCategory2":[{"jb_product_subcategory2_1_id":"2","jb_product_subcategory2_2_category_id":"2","jb_product_subcategory2_3_subcategory1_id":"2","jb_product_subcategory2_4_name":"Western","jb_product_subcategory2_5_delete_status":"0","jb_product_subcategory2_6_created_on":"1502429794573","jb_product_subcategory2_7_updated_on":"1502429794573"}]},{"jb_product_subcategory1_1_id":"3","jb_product_subcategory1_2_category_id":"2","jb_product_subcategory1_3_name":"Boys","jb_product_subcategory1_4_delete_status":"0","jb_product_subcategory1_5_created_on":"1505105190176","jb_product_subcategory1_6_updated_on":"1505105190176","subCategory2":[]}]}]}';
$array = json_decode($json_string, true);
function build_list($array) {
$list = '<ul>';
foreach($array as $key => $value) {
foreach($value as $key => $index) {
if(is_array($index)) {
$list .= build_list($index);
} else {
$list .= "<li>$index</li>";
}
}
}
$list .= '</ul>';
return $list;
}
echo build_list($array);
?>
Simply use only one foreach, the nested one seems useless :
foreach($array as $key => $index) {
if(is_array($index)) {
/* ... */
Just add a condition to check if $value is a valid array or not. This way it will not process $value if it's not an array and warnings will go away.
if (!is_array($value)) {
continue;
}
Use this condition inside foreach before the looping $array.
foreach($array as $key => $value) {
if (!is_array($value)) {
continue;
}
foreach($value as $k => $index) {
if(is_array($index)) {
$list .= build_list($index);
} else {
$list .= "<li>$index</li>";
}
}
}
Ideone link : Code
I'm working on this problem of sorting in php.
I have to write my own sorting functions, using my_asort() and my_ksort() functions that do exactly the same as asort and ksort respectively.
However I'm not able to get the correct output (I'm new to php), hence any help on how to correct this, would be greatly appreciated.
This is the code I typed up:
<?php
echo "Original Array<br><br>";
$member = array("Jack" => "55kg", "Bill" => "35kg", "Aaron" => "60kg", "Daniel" => "80kg" );
foreach ($member as $user => $weight) {
echo "$user = $weight <br>";
}
echo "<br><br>";
function my_asort($member)
{
$keys=array_keys($member);
sort($keys);
foreach($keys as $key)
{
$val=$member[$key];
unset($member[$key]);
$member[$key]=$val;
}
}
echo "Sorted By user <br><br>";
foreach ($member as $user => $weight) {
echo "$user = $weight <br>";
}
echo "<br><br>";
function my_ksort($member)
{
$keys=array_keys($member);
sort($keys);
foreach($keys as $key)
{
$val=$member[$key];
unset($member[$key]);
$member[$key]=$val;
}
}
echo "Sorted By weight <br><br>";
foreach ($member as $user => $weight) {
echo "$user = $weight <br>";
}
?>
This is what it displays: (It doesn't do any sorting, what am I doing wrong?)
Original Array
Jack = 55kg
Bill = 35kg
Aaron = 60kg
Daniel = 80kg
Sorted By user
Jack = 55kg
Bill = 35kg
Aaron = 60kg
Daniel = 80kg
Sorted By weight
Jack = 55kg
Bill = 35kg
Aaron = 60kg
Daniel = 80kg
You need to call your functions (or use the code without a function declaration). You just defined them inside your code.
So if you want to use them, you need to call them and your code will look like:
<?php
function my_ksort(&$member)
{
$keys=array_keys($member);
sort($keys);
foreach($keys as $key)
{
$val=$member[$key];
unset($member[$key]);
$member[$key]=$val;
}
}
function my_asort(&$member)
{
$keys=array_keys($member);
sort($keys);
foreach($keys as $key)
{
$val=$member[$key];
unset($member[$key]);
$member[$key]=$val;
}
}
echo "Original Array<br><br>";
$member = array("Jack" => "55kg", "Bill" => "35kg", "Aaron" => "60kg", "Daniel" => "80kg" );
foreach ($member as $user => $weight) {
echo "$user = $weight <br>";
}
echo "<br><br>";
my_asort($member);
echo "Sorted By user <br><br>";
foreach ($member as $user => $weight) {
echo "$user = $weight <br>";
}
echo "<br><br>";
my_ksort($member);
echo "Sorted By weight <br><br>";
foreach ($member as $user => $weight) {
echo "$user = $weight <br>";
}
Two problems :
You are not using the functions you created :
my_asort($member);
my_ksort($member);
And you'll have to add a reference (&) on your array in the parameters since you are not returning anything but modifying arrays :
function my_asort(&$member)
function my_ksort(&$member)
(I didn't check if the results correspond to the actual asort and ksort functions, but since it looks like a school homework, I'm sure you will find out now that you have an output of your functions)
You have not called methods anywhere in your code. You just provided the method definition but not calling.
Build your array like this:
$member[] = array('name' => 'Jake', 'weight' => '54');
$member[] = array('name' => 'Bill', 'weight' => '35');
$member[] = array('name' => 'Daniel', 'weight' => '80');
Then use the function bellow:
$new_arr = array_sort($member, 'name', 'SORT_ASC');
$new_arr2 = array_sort($member, 'weight', 'SORT_DESC');
print_r($new_arr);
print_r($new_arr2);
function array_sort($array, $on, $order='SORT_ASC'){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case 'SORT_ASC':
asort($sortable_array);
break;
case 'SORT_DESC':
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
This is a sample of my Json
{
"164616":{
"competitors":{
"950053":{
"Name":"Hookinn",
"Jockey":"L PILLAR(A)",
"DetailedPricing":{
"winTotePlus":false,
"placeTotePlus":false,
"placePrices":{
"2":0,
"12":0
},
"RisaSilkID":1551
}
}
}
}
}
This is my code .
$string = file_get_contents(#"D:/test.json");
$json_a=json_decode($string,true);
foreach ($json_a as $root_element => $childnode) {
foreach( $childnode as $cKey => $subChild) {
foreach( $subChild as $cKey2 => $subChild2) {
echo($subChild2['Name']);
echo("<br>");
foreach($subChild2['DetailedPricing'] as $compKey => $compVal) {
// echo($compVal['placeTotePlus']); echo("<br>");}
}
}
}
}
I am able to get Name attribute using subChild2['Name'] but i when i am trying to access placeTotePlus attribute which is inside DetailedPricing array then i am getting undefined index error. What i am doing wrong? and i also want to get placePrices values as well.
Thanks
Here you go.
$string = json_decode($string, true);
foreach($string as $item => $comp) {
foreach($comp as $key =>$users) {
foreach($users as $derp=>$user) {
var_dump($user['DetailedPricing']['placeTotePlus']);
}
}
}
Which returns:
bool(false)
Example
The reason it doesn't print anything is because it is a boolean. You can test (as stated in the comments) something like this:
(!$users['DetailedPricing']['placeTotePlus']) ? 'Its false' : 'Its true';
In response to your comment, you'd use isset()
if(!isset($user['DetailedPricing']['PlaceTotPlus'])) {
print 'nothing';
}
foreach doesn't fetch end of array. I want to use end of that in another method:
$array = array(
"Language programings"=>array("php" => 100,"js" => 200),'html'=>12);
foreach ($array as $kk=>$val1)
{
echo $kk.'<br/>';
foreach ($val1 as $key=>$val2)
{
if (! end(array_keys($array)))
echo $val2;
}
echo end(array_value);//must be show 12
}
If I understand correctly, in your if() statement, you're attempting to see if the pointer is at the end of the array. Unfortunately, in this case, end() will never be false and so the line echo $val2; won't ever execute.
Try replacing
if (! end(array_keys($array)))
with
if ($key <> end(array_keys($array))
also your last line should be:
echo end(array_values($array));
Try using below code:
$array = array("Language programings"=>array("php" => 100,"js" => 200),'html'=>12);
foreach($array as $key1 => $val1){
if(is_array($val1)){
echo $key1.'<br/>';
foreach($val1 as $key2 => $val2){
if(is_array($val2)){
foreach($val2 as $k => $v){
echo $v.'<br/>';
}
} else {
echo $val2.'<br/>';
}
}
}
}
echo end(array_values($array));
The result will be:
Language programings
100
200
12