/    Sign up×
Community /Pin to ProfileBookmark

values from associative array to indexed array?

I’m creating an indexed array from JUST values of an associative array (fetched from a database) which I can then reassign to different variables by index

`$runSql = mysqli_query($connect, $queryTitle);
if ($runSql) {
if (mysqli_num_rows($runSql) > 0) {
$indxarrTitleResult = array();
while ($assocarrResultTitle = mysqli_fetch_assoc($runSql)) {
$indxarrTitleResult[] = $assocarrResultTitle[‘Title’];
}
print_r($indxarrTitleResult);
}
}`

this produces:

Array ( [0] => Traditional Favourites [1] => Starters [2] => Mains )

but what I really want is:

Array ( “Traditional Favourites”, “Starters”, “Mains”)

what is going wrong? From the result I got, I can only assign Traditional Favourites to another variable but nothing else

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 10.2019 — Not sure what you really want to happen, but by definition every element of an array has an index/key, and print_r() will always show those indexes/keys if the argument is an array.
<i>
</i>$ php -a
Interactive shell

php &gt; $test = array("Traditional Favourites", "Starters", "Mains");
php &gt; print_r($test);
Array
(
[0] =&gt; Traditional Favourites
[1] =&gt; Starters
[2] =&gt; Mains
)
php &gt; echo implode(', ', $test)."n";
Traditional Favourites, Starters, Mains
php &gt;
Copy linkTweet thisAlerts:
@ginerjmMay 10.2019 — Your very first statement talks about creating an "indexed array" and using an index to retrieve values from it. Now you want to get rid of the very thing that you wanted to obtain?

All arrays have indices - be they numbers or alphanumeric ones. There will always be an index for each and every element., ie value.
Copy linkTweet thisAlerts:
@nsathauthorMay 10.2019 — @NogDog#1603526 print_r will show the indexes/keys but shouldn't they be delimited using commas?
Copy linkTweet thisAlerts:
@nsathauthorMay 10.2019 — @ginerjm#1603527 I don't want to get rid of them. but I want assign those values in the array to other variables

$headerTraditionalFavs = $indxarrTitleResult[0];
$headerStarters = $indxarrTitleResult[1];
$headerMains = $indxarrTitleResult[2];


these I would then echo out in div at different points in the page but the only that shows is the $headerStarters = $indxarrTitleResult[1], everything else shows a blank
Copy linkTweet thisAlerts:
@NogDogMay 10.2019 — > @nsath#1603560 print_r will show the indexes/keys but shouldn't they be delimited using commas?

No, they're actually delimited with newlines, as in my example. But that should be beside the point: it's mainly a debug function -- if you want to output an array in a specific format, you should control it explicitly, as I did in a fairly simple manner with the implode() function.
Copy linkTweet thisAlerts:
@ginerjmMay 10.2019 — Since you have an associative array (from the query) you could simply do a fetchall and keep that array around to use instead of using a new set of variables that you have to load from the array that you did the fetchall to. You would simply use the array with an index that apparently matches the name of the variable that you are needlessly creating.
Copy linkTweet thisAlerts:
@nsathauthorMay 10.2019 — @NogDog#1603563 yeh I just found that out now aswell thanks
Copy linkTweet thisAlerts:
@nsathauthorMay 10.2019 — @ginerjm#1603565 Thanks for the pointer got it, I didn't think along that path.
Copy linkTweet thisAlerts:
@ginerjmMay 10.2019 — None of us do at first. HTH!
×

Success!

Help @nsath spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...