I have a problem i need to ignore any array key if it has "lastpost" set to "No Posts"
This is the array dump of $sublast:
array(3) { [0]=> array(9) { ["forum_category_id"]=> string(1) "3" ["name"]=> string(15) "Sub category #1" ["description"]=> string(39) "This is a short description test.... #2" ["topics"]=> string(1) "0" ["lastpost"]=> string(8) "No Posts" ["lastpostauthor"]=> string(1) " " ["lastposturl"]=> string(88) "http://test.codetrove.com/index.php?route=forum/read&forum_path=3&forum_post_id=" ["posts"]=> int(0) ["href"]=> string(81) "http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=1_3" } [1]=> array(9) { ["forum_category_id"]=> string(1) "7" ["name"]=> string(16) "Test sub Sub cat" ["description"]=> string(0) "" ["topics"]=> string(1) "2" ["lastpost"]=> string(26) "Mon Apr 1, 2013 1:00:42 pm" ["lastpostauthor"]=> string(16) "Justine Smithies" ["lastposturl"]=> string(90) "http://test.codetrove.com/index.php?route=forum/read&forum_path=7&forum_post_id=63" ["posts"]=> int(2) ["href"]=> string(81) "http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=1_7" } [2]=> array(9) { ["forum_category_id"]=> string(1) "9" ["name"]=> string(11) "Test cat #3" ["description"]=> string(20) "Short description #3" ["topics"]=> string(1) "2" ["lastpost"]=> string(26) "Mon Apr 1, 2013 1:00:15 pm" ["lastpostauthor"]=> string(16) "Justine Smithies" ["lastposturl"]=> string(90) "http://test.codetrove.com/index.php?route=forum/read&forum_path=9&forum_post_id=62" ["posts"]=> int(2) ["href"]=> string(81) "http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=1_9" } }
Here is the code i am trying to do that only it messes up as it always goes for the top one which is right but i need a way to remove the top entries if the have "lastpost" set to "No Posts" so it get the correct info .
<?php
$sublast = $forum_category['children'];
foreach ($sublast as $key => $row) {
$dates[$key] = $row['lastpost'];
}
array_multisort($dates, SORT_ASC, $sublast);
$lastitem = $sublast[count($sublast) - 1];
var_dump($sublast);
?>
This is the dump of $lastitem :
array(9) { ["forum_category_id"]=> string(1) "3" ["name"]=> string(15) "Sub category #1" ["description"]=> string(39) "This is a short description test.... #2" ["topics"]=> string(1) "0" ["lastpost"]=> string(8) "No Posts" ["lastpostauthor"]=> string(1) " " ["lastposturl"]=> string(88) "http://test.codetrove.com/index.php?route=forum/read&forum_path=3&forum_post_id=" ["posts"]=> int(0) ["href"]=> string(81) "http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=1_3" }
And as you can see it took the "No Posts" when that whole array entry should be removed so it takes array[1] Which is the one i need.
Any ideas please ?
Use unset function in php to delete elements
Unset will do your need.
unset($array)
Related
array(2) {
[0]=>
object(stdClass)#21 (7) {
["id"]=>
string(1) "1"
["title"]=>
string(7) "cvxzcvd"
["con"]=>
string(10) "gvsdvgsdfg"
["is_important"]=>
string(1) "1"
["date"]=>
string(3) "123"
["image"]=>
string(2) "55"
["cat_id"]=>
string(1) "1"
}
[1]=>
object(stdClass)#22 (7) {
["id"]=>
string(1) "2"
["title"]=>
string(4) "fsdf"
["con"]=>
string(9) "dfdsfvfds"
["is_important"]=>
string(1) "1"
["date"]=>
string(4) "5145"
["image"]=>
string(7) "5454124"
["cat_id"]=>
string(1) "2"
}
}
I passed this array into a view
$this->load->view('home/index',$news_data);
but I wanna use the data separately.
I mean if I wanna use the second title
or the second data.
how can I express that in the view
thanks
You can go like this:-
$news_data[1]->title;
$news_data[1]->data;
Store the array into variable like
$news_data['arr'] = { } ;
,and in view file access it by $arr;
print_r($arr);
I have an if statment below that checks if a value is present in an array and adds it in there if it's not.
Apparently even if the value is in the array, it triggers it again.
As far as I understand, it should only display 1 of each value, because it will only trigger 3 times, like this:
Digital Photography -> 0
Step by Step Macintosh Training -> 0
How to become a Powerful Speaker -> 0
The code:
if (!in_array($unit['course_name'], $courseList)) {
$courseList[$unit['course_name']]['name'] = $unit['course_name'];
$courseList[$unit['course_name']]['seconds'] = 0;
echo $courseList[$unit['course_name']]['name'] . ' -> ' . $courseList[$unit['course_name']]['seconds'];
echo "<BR>";
}
But it outputs:
Digital Photography -> 0
Step by Step Macintosh Training -> 0
Step by Step Macintosh Training -> 0
Step by Step Macintosh Training -> 0
How to become a Powerful Speaker -> 0
How to become a Powerful Speaker -> 0
Here is the var_dump($unit):
array(8) { ["author_name"]=> string(10) "tuiuiu_dev" [0]=> string(10) "tuiuiu_dev" ["course_name"]=> string(19) "Digital Photography" [1]=> string(19) "Digital Photography" ["unit_id"]=> string(3) "181" [2]=> string(3) "181" ["unit_quantity"]=> string(1) "1" [3]=> string(1) "1" }
array(8) { ["author_name"]=> string(15) "William Merussi" [0]=> string(15) "William Merussi" ["course_name"]=> string(31) "Step by Step Macintosh Training" [1]=> string(31) "Step by Step Macintosh Training" ["unit_id"]=> string(3) "227" [2]=> string(3) "227" ["unit_quantity"]=> string(1) "1" [3]=> string(1) "1" }
array(8) { ["author_name"]=> string(15) "William Merussi" [0]=> string(15) "William Merussi" ["course_name"]=> string(31) "Step by Step Macintosh Training" [1]=> string(31) "Step by Step Macintosh Training" ["unit_id"]=> string(3) "231" [2]=> string(3) "231" ["unit_quantity"]=> string(1) "1" [3]=> string(1) "1" }
array(8) { ["author_name"]=> string(15) "William Merussi" [0]=> string(15) "William Merussi" ["course_name"]=> string(31) "Step by Step Macintosh Training" [1]=> string(31) "Step by Step Macintosh Training" ["unit_id"]=> string(3) "233" [2]=> string(3) "233" ["unit_quantity"]=> string(1) "1" [3]=> string(1) "1" }
array(8) { ["author_name"]=> string(10) "tuiuiu_dev" [0]=> string(10) "tuiuiu_dev" ["course_name"]=> string(32) "How to become a Powerful Speaker" [1]=> string(32) "How to become a Powerful Speaker" ["unit_id"]=> string(4) "1080" [2]=> string(4) "1080" ["unit_quantity"]=> string(1) "1" [3]=> string(1) "1" }
array(8) { ["author_name"]=> string(10) "tuiuiu_dev" [0]=> string(10) "tuiuiu_dev" ["course_name"]=> string(32) "How to become a Powerful Speaker" [1]=> string(32) "How to become a Powerful Speaker" ["unit_id"]=> string(4) "1084" [2]=> string(4) "1084" ["unit_quantity"]=> string(1) "1" [3]=> string(1) "1" }
Thank you for any help!
in_array() method check array values, not keys. From your example I can see that as value you have an array with name and seconds fields.
I think you want to check if that id exists in your array.
So this if:
if (!in_array($unit['course_name'], $courseList)) {
Should be change like this:
if (!isset($courseList[$unit['course_name']])) {
This way you check if the $unit['course_name'] is in your array as key.
If you have an array with only values you should use in_array function as this example:
$array = array("A", "B", "C");
in_array($string, $array) ? "found" : "not found";
Else if you have an array with keys you should use isset function as this example:
$array = array("A" => "Result A", "B" => "Result B", "C" => "Result C");
isset($array[$string]) ? "found" : "not found";
I am having a hard time extracting a value from the following JSON object
array(3) { [0]=> object(stdClass)#1 (11) { ["Group"]=> string(2) "18" ["GroupName"]=> string(8) "Wireline" ["Region"]=> string(2) "15" ["RegionName"]=> string(8) "Atlantic" ["Province"]=> string(1) "1" ["ProvinceName"]=> string(13) "New Brunswick" ["City"]=> string(2) "11" ["CityName"]=> string(10) "Campbelton" ["Site"]=> string(2) "37" ["SiteName"]=> string(16) "Campbellton PNCT" ["Year"]=> string(4) "2016" }
[1]=> object(stdClass)#2 (5) { ["PlatformID"]=> string(1) "1" ["PlatformTag"]=> string(6) "Access" ["Rack"]=> string(24) "23" Width 36" Depth Rack" ["RackValue"]=> string(1) "2" ["Comments"]=> string(0) "" }
[2]=> object(stdClass)#3 (12) { ["Rack"]=> string(31) "23" Width 36" Depth Rack Access" ["RackValue"]=> string(1) "2" ["RackComments"]=> string(0) "" ["Manufacturer"]=> string(6) "werwer" ["Name"]=> string(6) "werwer" ["Quantity"]=> string(1) "1" ["RackUnits"]=> string(1) "1" ["Power"]=> string(1) "1" ["ActivePassive"]=> string(6) "Active" ["ACDC"]=> string(2) "AC" ["ConnectivityIDs"]=> array(1) { [0]=> string(1) "2" } ["Connectivity"]=> array(1) { [0]=> string(5) "Fiber" } } }
I am trying to extract each item in a foreach loop within PHP Above is the var_dump of the $data[0] JSON object it demonstrates what the Array item looks like.
My foreach is the following
$data = json_decode($_POST["submitdata"]);
$Forecasts = $data[0];
foreach($Forecasts as $Forecast){
echo($Forecast->PlatformID);}
but it returns nothing as a result. Can someone explain to me how to get this from the second Array in the object?
simply place the Index of the inner array along with the object as shown below. This will check for the sub item for the PlatformID and return it to the screen.
foreach($Forecasts as $Forecast){
echo($Forecast[1]->PlatformID);}
I have the array like this:
} ["items":"Jcart":private]=> array(3) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
[2]=>
string(7) "ABC-123" }
How to get the "items" values in a php variable?
///////
Te complete object is:
object(Jcart)#1 (8) {
["config"]=>
array(12) {
["jcartPath"]=>
string(6) "jcart/"
["checkoutPath"]=>
string(12) "checkout.php"
["item"]=>
array(6) {
["id"]=>
string(10) "my-item-id"
["name"]=>
string(12) "my-item-name"
["price"]=>
string(13) "my-item-price"
["qty"]=>
string(11) "my-item-qty"
["url"]=>
string(11) "my-item-url"
["add"]=>
string(13) "my-add-button"
}
["paypal"]=>
array(5) {
["id"]=>
string(38) "seller_1282188508_biz#conceptlogic.com"
["https"]=>
bool(true)
["sandbox"]=>
bool(false)
["returnUrl"]=>
string(0) ""
["notifyUrl"]=>
string(0) ""
}
["currencyCode"]=>
string(3) "USD"
["csrfToken"]=>
bool(false)
["text"]=>
array(14) {
["cartTitle"]=>
string(13) "Shopping Cart"
["singleItem"]=>
string(4) "Item"
["multipleItems"]=>
string(5) "Items"
["subtotal"]=>
string(8) "Subtotal"
["update"]=>
string(6) "update"
["checkout"]=>
string(8) "checkout"
["checkoutPaypal"]=>
string(20) "Checkout with PayPal"
["removeLink"]=>
string(6) "remove"
["emptyButton"]=>
string(5) "empty"
["emptyMessage"]=>
string(19) "Your cart is empty!"
["itemAdded"]=>
string(11) "Item added!"
["priceError"]=>
string(21) "Invalid price format!"
["quantityError"]=>
string(38) "Item quantities must be whole numbers!"
["checkoutError"]=>
string(34) "Your order could not be processed!"
}
["button"]=>
array(4) {
["checkout"]=>
string(0) ""
["paypal"]=>
string(0) ""
["update"]=>
string(0) ""
["empty"]=>
string(0) ""
}
["tooltip"]=>
bool(true)
["decimalQtys"]=>
bool(false)
["decimalPlaces"]=>
int(1)
["priceFormat"]=>
array(3) {
["decimals"]=>
int(2)
["dec_point"]=>
string(1) "."
["thousands_sep"]=>
string(1) ","
}
}
["items":"Jcart":private]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
}
["names":"Jcart":private]=>
array(2) {
[3]=>
string(12) "Hockey Stick"
[2]=>
string(13) "Baseball Mitt"
}
["prices":"Jcart":private]=>
array(2) {
[3]=>
string(5) "33.25"
[2]=>
string(5) "19.50"
}
["qtys":"Jcart":private]=>
array(2) {
[3]=>
string(1) "1"
[2]=>
string(2) "20"
}
["urls":"Jcart":private]=>
array(2) {
[3]=>
string(15) "http://bing.com"
[2]=>
string(16) "http://yahoo.com"
}
["subtotal":"Jcart":private]=>
float(423.25)
["itemCount":"Jcart":private]=>
int(21)
}
I just need the values in ["items":"Jcart":private]
I assume you got this by casting an object to an array. If that's the case, you shouldn't be doing that! The property is private because you're not supposed to access it directly because it's not a public API. The object should have a public method which you're supposed to call instead, that's the public API. Something like $foo->getItems() or such. Read the documentation and/or source code.
I want to get data from an array using PHP loop like FOR or FOREACH.
when I print the content of the array using var_dump it looks like this:
array(2) {
[0]=>
array(2) {
["name"]=>
string(6) "query1"
["fql_result_set"]=>
array(13) {
[0]=>
array(3) {
["aid"]=>
string(19) "2944783819003364347"
["name"]=>
string(16) "Profile Pictures"
["photo_count"]=>
string(2) "37"
}
[1]=>
array(3) {
["aid"]=>
string(19) "2944783820076875780"
["name"]=>
string(7) "only me"
["photo_count"]=>
string(1) "2"
}
[2]=>
array(3) {
["aid"]=>
string(19) "2944783819003517141"
["name"]=>
string(35) "Ways To Tie Your Shoelaces. Nice :)"
["photo_count"]=>
string(1) "8"
}
[3]=>
array(3) {
["aid"]=>
string(19) "2944783819003490957"
["name"]=>
string(12) "Cover Photos"
["photo_count"]=>
string(2) "12"
}
[4]=>
array(3) {
["aid"]=>
string(19) "2944783819003481818"
["name"]=>
string(14) "Mobile Uploads"
["photo_count"]=>
string(2) "55"
}
}
}
what should I do to get: ["aid"] , ["name" ] and ["photo_count"]?
like this
foreach ($array[0]['fql_result_set'] as $record) {
echo "{$record['aid']}, {$record['name']}";
}